`
icheng
  • 浏览: 882009 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
文章分类
社区版块
存档分类
最新评论

iPhone多视图开发案例纪实

 
阅读更多

本文介绍了iPhone多视图开发的一个案例。基本上这就是一个项目的完成笔记,希望能和大家一起分享提高。


本文是iPhone多视图开发和WebService客户端技术实现的一个案例介绍。文中以一个简单的例子来说明iPhone多视图开发。

1.新建iPhone项目

打开XCode,新建IPhone项目,选择“window-based Application”模板,项目名称暂定为shouji138,效果如下图:

新建IPhone项目

完成之后的界面如下图:

iPhone多视图开发

2.添加控制视图的ViewController类:SwitchViewController;

这里用一个ViewController来负责整个程序的视图跳转,这样控制起来就很方便了。

在XCode左边“Classes”上面点右键,“Add”->“New File...”,选择“Cocoa Touch Class”->“UIViewController subclass”,取名为SwitchViewController,如下图:

iPhone多视图开发

iPhone多视图开发

3.添加第一个视图的控制类FirstViewController

添加第一个视图控制类,命名为FirstViewController,方法同上,如下图:

添加第一个视图的控制类

4.添加第一个视图,FirstView.xib

添加视图文件,在Resources上面新建文件,选择“User Interface”->“View XIB”,输入名称“FirstView”,如下图:

iPhone多视图开发

iPhone多视图开发

5.连接好FirstView的对应关系

添加了视图控制类和XIB文件之后,需要将他们关联起来。方法如下:

双击新建的“FirstView.xib”文件,打开界面设计器,选择"FirstView.xib"的属性面板,选中“File's Owner”,如下图:

iPhone多视图开发

选中菜单“Tools”->“Inspector”,调出属性窗口,选中最后一个标签栏,在“Class”下,选中“FirstViewController”,如下图:

iPhone多视图开发

在第二个标签栏,选中“Outlets”下的view,用鼠标拖曳它到FirstView.xib属性窗口中的“View”上面,如下图:

iPhone多视图开发

6.在FirstView.xib上添加控件

选择菜单“Tools”->“Library”,调出控件库,拖一个Label和Button到设计窗口,效果如下图:

iPhone多视图开发

7.添加第二个视图:SecondViewController和SecondView.xib

如法炮制添加第二个视图,方法同上。

8.连接好SecondView的对应关系

如法炮制连接好ViewController和View。

9.在SecondView.xib添加控件

如法炮制添加好控件,如下图。

iPhone多视图开发

10.在控制类SwitchViewController添加代码,实现对2个视图的跳转。

在SwitchViewController.h中添加代码:

  1. //
  2. // SwitchViewController.h
  3. // shouji138.com 手机主题
  4. //
  5. // Created by administrator on 8/27/09.
  6. // Copyright 2009 __MyCompanyName__. All rights reserved.
  7. //
  8. #import
  9. @class FirstViewController;
  10. @class SecondViewController;
  11. @interface SwitchViewController : UIViewController {
  12. FirstViewController* firstviewcontroller;
  13. SecondViewController* secondviewcontroller;
  14. }
  15. @property (nonatomic,retain) FirstViewController* firstviewcontroller;
  16. @property (nonatomic,retain) SecondViewController* secondviewcontroller;
  17. -(void)initView;
  18. -(void)showFirstView;
  19. -(void)showSecondView;
  20. -(void)removeAllView;
  21. @end
  22. 说明一下:
  23. initView 方法用来程序加载时初始化view,showFirstView方法用来显示第一个view,showSecondView用来显示第二view。
  24. 在SwitchViewController.m中添加代码:
  25. //
  26. // SwitchViewController.m
  27. // shouji138.com 手机主题
  28. //
  29. // Created by administrator on 8/27/09.
  30. // Copyright 2009 __MyCompanyName__. All rights reserved.
  31. //
  32. #import "SwitchViewController.h"
  33. #import "FirstViewController.h"
  34. #import "SecondViewController.h"
  35. @implementation SwitchViewController
  36. @synthesize firstviewcontroller;
  37. @synthesize secondviewcontroller;
  38. -(void)initView{
  39. NSLog(@"ttt");
  40. if(self.firstviewcontroller == nil){
  41. self.firstviewcontroller = [[FirstViewController alloc]initWithNibName:@"FirstView" bundle:nil];
  42. }
  43. [self removeAllView];
  44. [self.view insertSubview:self.firstviewcontroller.view atIndex:0];
  45. }
  46. -(void)showFirstView{
  47. if(self.firstviewcontroller == nil){
  48. self.firstviewcontroller = [[FirstViewController alloc]initWithNibName:@"FirstView" bundle:nil];
  49. }
  50. [self removeAllView];
  51. [self.view insertSubview:self.firstviewcontroller.view atIndex:0];
  52. }
  53. -(void)showSecondView{
  54. if(self.secondviewcontroller == nil){
  55. self.secondviewcontroller = [[SecondViewController alloc]initWithNibName:@"SecondView" bundle:nil];
  56. }
  57. [self removeAllView];
  58. [self.view insertSubview:self.secondviewcontroller.view atIndex:0];
  59. }
  60. -(void)removeAllView{
  61. for(NSInteger i=0;i<[self.view.subviews count];i++){
  62. [[self.view.subviews objectAtIndex:i] removeFromSuperview];
  63. }
  64. }
  65. /*
  66. // The designated initializer. Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
  67. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  68. if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  69. // Custom initialization
  70. }
  71. return self;
  72. }
  73. */
  74. /*
  75. // Implement loadView to create a view hierarchy programmatically, without using a nib.
  76. - (void)loadView {
  77. }
  78. */
  79. /*
  80. // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
  81. - (void)viewDidLoad {
  82. [super viewDidLoad];
  83. }
  84. */
  85. /*
  86. // Override to allow orientations other than the default portrait orientation.
  87. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  88. // Return YES for supported orientations
  89. return (interfaceOrientation == UIInterfaceOrientationPortrait);
  90. }
  91. */
  92. - (void)didReceiveMemoryWarning {
  93. // Releases the view if it doesn't have a superview.
  94. [super didReceiveMemoryWarning];
  95. // Release any cached data, images, etc that aren't in use.
  96. }
  97. - (void)viewDidUnload {
  98. // Release any retained subviews of the main view.
  99. // e.g. self.myOutlet = nil;
  100. }
  101. - (void)dealloc {
  102. [firstviewcontroller release];
  103. [secondviewcontroller release];
  104. [super dealloc];
  105. }
  106. @end

11.修改shouji138AppDelegate代码

修改shouji138AppDelegate.h,代码如下:

  1. //
  2. // shouji138AppDelegate.h
  3. // shouji138.com 手机主题
  4. //
  5. // Created by administrator on 8/27/09.
  6. // Copyright __MyCompanyName__ 2009. All rights reserved.
  7. //
  8. #import
  9. @class SwitchViewController;
  10. @interface shouji138AppDelegate : NSObject {
  11. IBOutlet UIWindow *window;
  12. IBOutlet SwitchViewController *viewController;
  13. }
  14. @property (nonatomic, retain) UIWindow *window;
  15. @property (nonatomic, retain) SwitchViewController *viewController;
  16. +(shouji138AppDelegate *)App;
  17. @end
  18. 修改shouji138AppDelegate.m代码如下:
  19. //
  20. // shouji138AppDelegate.m
  21. // shouji138.com 手机主题下载
  22. //
  23. // Created by administrator on 8/27/09.
  24. // Copyright __MyCompanyName__ 2009. All rights reserved.
  25. //
  26. #import "shouji138AppDelegate.h"
  27. #import "SwitchViewController.h"
  28. @implementation shouji138AppDelegate
  29. @synthesize window;
  30. @synthesize viewController;
  31. - (void)applicationDidFinishLaunching:(UIApplication *)application {
  32. // Override point for customization after application launch
  33. [window addSubview:viewController.view];
  34. [viewController initView];
  35. [window makeKeyAndVisible];
  36. }
  37. +(shouji138AppDelegate *)App{
  38. return (shouji138AppDelegate *)[[UIApplication sharedApplication]delegate];
  39. }
  40. - (void)dealloc {
  41. [window release];
  42. [viewController release];
  43. [super dealloc];
  44. }
  45. @end

其中:applicationDidFinishLaunching 方法中调用了SwitchViewController的initView方法,把第一个视图FirstView加载到了屏幕中,因此程序运行之后,我们看到的第一个页面是FirstView。

选择菜单“Build”->“Build”,进行编译,如果没有问题,应该可以编译通过。

12.在MainWindow.xib中连接好与SwitchViewController的对应关系。

这一步是非常重要的。

双击“MainWindow.xib”,调出“Interface Builder”;

从Library控件库中,拖动一个view Controller到“MainWindow.xib”窗口;

iPhone多视图开发

将这个添加的view Controller的Class设置为SwitchViewController;

iPhone多视图开发

选择“Shouji138 APP Delegate”,在“Outlets”->“viewController”中,拖曳一个连接线到“Switch View Controller”;

iPhone多视图开发

到此,完成了最重要的部分了,保存之后,点击“Build and Go”,应该会出现第一个页面。

13.添加FirstViewController和SecondViewController代码

修改FirstViewController.h如下:

  1. //
  2. // FirstViewController.h
  3. // shouji138.com
  4. //
  5. // Created by administrator on 8/27/09.
  6. // Copyright 2009 __MyCompanyName__. All rights reserved.
  7. //
  8. #import
  9. @interface FirstViewController : UIViewController {
  10. }
  11. -(IBAction)buttonClick:(id)sender;
  12. @end
  13. 修改FirstViewController.m如下
  14. //
  15. // FirstViewController.m
  16. // shouji138.com
  17. //
  18. // Created by administrator on 8/27/09.
  19. // Copyright 2009 __MyCompanyName__. All rights reserved.
  20. //
  21. #import "FirstViewController.h"
  22. #import "shouji138AppDelegate.h"
  23. #import "SwitchViewController.h"
  24. @implementation FirstViewController
  25. -(IBAction)buttonClick:(id)sender{
  26. [[shouji138AppDelegate App].viewController showSecondView];
  27. }
  28. ....中间省略.....
  29. - (void)dealloc {
  30. [super dealloc];
  31. }
  32. @end
  33. 修改SecondViewController.h如下:
  34. //
  35. // SecondViewController.h
  36. // shouji138.com
  37. //
  38. // Created by administrator on 8/27/09.
  39. // Copyright 2009 __MyCompanyName__. All rights reserved.
  40. //
  41. #import
  42. @interface SecondViewController : UIViewController {
  43. }
  44. -(IBAction)buttonClick:(id)sender;
  45. @end
  46. 修改SecondViewController.m如下:
  47. //
  48. // SecondViewController.m
  49. // shouji138.com
  50. //
  51. // Created by administrator on 8/27/09.
  52. // Copyright 2009 __MyCompanyName__. All rights reserved.
  53. //
  54. #import "SecondViewController.h"
  55. #import "shouji138AppDelegate.h"
  56. #import "SwitchViewController.h"
  57. @implementation SecondViewController
  58. -(IBAction)buttonClick:(id)sender{
  59. [[shouji138AppDelegate App].viewController showFirstView];
  60. }
  61. ....中间省略.....
  62. - (void)dealloc {
  63. [super dealloc];
  64. }
  65. @end

编译一下。

14.连接输出口

双击“FirstView.xib”,进入“Interface Builder”,选择“Show Second”按钮,选择“Button Connections”->“Events”->“Touch Up Inside”,拖出连接线到“File's Owner”,选择输出口“buttonClick”,效果如下图:

iPhone多视图开发

按照同样的设置,将SecondView.xib的“Show First”按钮事件连接到SecondViewController的buttonClick方法。

15.运行调试

点击“Build and Go”,在模拟器上出现第一个页面,点击“Show Second”按钮,跳转到第二个页面,点击“Show First”按钮,跳转到第一个页面。

iPhone多视图开发iPhone多视图开发


分享到:
评论

相关推荐

    iPhone多视图开发案例图文步骤

    ### iPhone多视图开发案例详解 #### 一、引言 在进行iPhone应用程序开发时,多视图切换是一项重要的技术,对于用户体验有着直接的影响。本文将详细介绍如何在iPhone项目中实现多个视图间的平滑切换,并通过实际...

    iPhone多视图切换

    在iOS开发中,多视图切换是构建用户界面的关键部分,尤其在iPhone应用设计中,它使得用户能够方便地在不同的功能或数据集之间导航。本教程将专注于使用Objective-C或Swift实现iPhone上的多视图切换,特别是通过窗口...

    iPhone 开发多视图切换 代码

    在iOS开发中,多视图切换是构建用户界面的关键部分,尤其对于iPhone应用程序而言,它提供了丰富的用户体验。本文将深入探讨如何在iPhone应用中实现多视图切换,并提供相关的代码示例。 首先,理解基本的视图(View...

    iPhone开发【十二】多视图技术总结之四:Segmented Control

    在"iPhone开发【十二】多视图技术总结之四:Segmented Control"这篇博文中,作者详细介绍了如何使用Segmented Control来控制多个视图的显示和隐藏,从而实现视图间的平滑过渡。 首先,我们需要在Interface Builder...

    iphone视图切换的控制

    在iOS应用开发中,视图切换是用户界面交互的核心部分,尤其对于iPhone应用而言,顺畅的视图切换体验直接影响到用户的使用感受。本教程将深入探讨如何在iPhone应用中实现视图之间的平滑切换,主要关注`Push`操作,这...

    iPhone 的高级视图应用开发教程

    【开发 iPhone 高级视图】教程主要涵盖了在iPhone应用程序开发中如何构建高效且用户体验良好的视图和表单。在这一教程中,作者Noel Rappin,一位Rails Development的副总裁,介绍了如何利用Ruby on Rails和Eclipse为...

    iPhone开发【十一】多视图技术总结之三:Page Control

    "iPhone开发【十一】多视图技术总结之三:Page Control"这个主题聚焦于使用UIPageControl来实现滑动浏览多个页面的效果,这在许多应用中常见,如相册、教程或者轮播广告等。UIPageControl是iOS SDK中的一个控件,它...

    iPhone开发【十】多视图技术总结之二:Navigation

    在"iPhone开发【十】多视图技术总结之二:Navigation"中,博主可能详细讲解了以下几点: 1. **Navigation Bar**:导航控制器顶部的导航栏,显示了当前视图的标题,并且可以包含左侧和右侧的Bar Button Items,用于...

    iphone 各种视图切换效果

    在iOS开发中,iPhone应用程序的用户体验往往离不开各种视图(View)之间的切换效果。这些效果不仅提升了用户界面的美观度,还能提供更好的交互体验。本文将深入探讨“iPhone各种视图切换效果”,并结合源码分析,...

    iphone表视图的创建于使用

    在iOS应用开发中,表视图(UITableView)是不可或缺的一部分,尤其在iPhone应用设计中扮演着重要角色。它提供了一种高效、灵活的方式来展示大量结构化的数据,如联系人列表、菜单选项或日历事件。本篇文章将深入探讨...

    iphone_开发_之_窗口和视图

    ### iPhone开发之窗口和视图 #### 一、引言 在iPhone应用程序开发过程中,窗口和视图扮演着至关重要的角色。它们不仅是构建用户界面的基础元素,还是与用户交互的关键通道。本文旨在深入探讨窗口和视图的概念及其...

    iPhone开发【八】多视图技术总结之一:ModalView(模态视图)

    本文将深入探讨"iPhone开发【八】多视图技术总结之一:ModalView(模态视图)"这一主题,旨在帮助开发者更好地理解和运用模态视图在实际项目中的应用。 首先,模态视图(Modal View)是一种在当前视图之上显示一个...

    iOS iphone工具栏创建简单的多视图

    在iOS开发中,创建一个带有多个视图的iPhone应用程序是一个常见的需求,这通常涉及到界面的交互性和用户体验的设计。本教程将深入讲解如何在iPhone应用中使用工具栏(Toolbar)来实现简单的多视图切换,这对于初学者...

    最新 iPhone 应用程序开发全教程.pdf

    ### 最新iPhone应用程序开发全教程知识点总结 #### 一、教程基本信息 - **书名**:《最新 iPhone 应用程序开发全教程》 - **英文名称**:Beginning iPhone 3 Development Exploring the iPhone SDK - **作者**:...

    iPhone开发实战.pdf

    根据提供的文件信息,本文将对“iPhone开发实战”这一主题进行深入探讨,涵盖iPhone应用开发的基础概念、开发环境搭建、关键技术点以及实际案例分析等方面。 ### 一、iPhone开发概述 #### 1.1 iPhone应用开发简介 ...

    《Iphone开发基础教程》第六章 多视图应用程序

    《Iphone开发基础教程》第六章 多视图应用程序

    iPhone OS 视图控制器编程指南(第一章)

    导航控制器(Navigation Controller)和标签栏控制器(Tab Bar Controller)就是典型的容器视图控制器,它们提供了内置的导航和切换机制,简化了多视图管理的复杂度。 3. **模态视图控制器**(Modal View ...

    iphone sdk3 开发指南 源代码

    《iPhone SDK3 开发指南 源代码》 在iOS应用开发的世界中,Apple的iPhone SDK(Software Development Kit)扮演着至关重要的角色。SDK3,是Apple为开发者提供的第三个主要版本,它带来了许多新功能、改进和优化,...

    《Iphone开发基础教程》第八章 表视图简介(二)

    在iOS应用开发中,表视图(UITableView)是不可或缺的一部分,尤其在iPhone开发中更是频繁使用。本教程的第八章“表视图简介(二)”深入探讨了如何利用表视图来构建用户界面,提供了丰富的实践案例。下面将详细阐述...

Global site tag (gtag.js) - Google Analytics