今天看了一下iOS开发中多个视图的应用. iOS下包含了几种不同种类的视图类型和controller:比如Tab bar,Navigation bar,Tool bar等.也可以自定义自己的视图的controller 程序中主窗口的视图控制器我们成为root controller,由它负责不同视图的切换等功能. 由root controller负责的视图都有自己的controller和delegate,比如一个tab bar,当用户在tab bar上点击的时候,是由tab bar的controller负责处理,而当用户在内容界面点击的时候,是由内容视图的controller负责处理的. 书中的例子很简单,点击tab bar中的按扭,在两个背景颜色(一蓝一黄)的视图中切换,两个视图中各有一个button 书中的例子建立的步骤如下: 1.建立一个Window base application,没有vIEw controller,只有一个window 2.添加视图文件 2.1 新建文件,选择cocoa touch下的UIVIEwController subclass 不使用xib文件(书中这里是xcode4.2以前的版本) 保存为SwitchVIEwController.h 和 SwitchVIEwController.m.这就是root controller 2.2 按照步骤2.1,创建蓝,黄背景视图的类文件 BlueVIEwController & YellowVIEwController 3.添加xib文件 3.1 选择cocoa touch下user interface下的VIEw XIB的文件 3.2 新建两个视图的xib文件,BlueVIEw.xib & YellowVIEw.xib 4.在AppDelegate中添加一个IBOutlet, @property (nonatomic,retain) IBOutlet SwitchVIEwController *switchVIEwController; 5.为了将主视图SwitchVIEwController和Window关联,需要使用addSubvIEw,添加了以下代码: #import "VIEw_SwitcherAppDelegate.h" #import "SwitchVIEwController.h" @implementation VIEw_SwitcherAppDelegate @synthesize window; @synthesize switchVIEwController; - (voID) application:(UIApplication *)application dIDFinishLaunchingWithOptions:(NSDictionary *)launchOptions{ // OverrIDe point for customization after application launch [self.window addSubvIEw:switchVIEwController.vIEw]; [self.window makeKeyAndVisible]; return YES; //注:书中代码这样写的,返回YES 但是函数类型是voID.经查SDK,发现函数类型是BOol } - (voID)dealloc { [window release]; [switchVIEwController release]; [super dealloc]; } 6. 为了切换两个vIEw,我们在SwitchVIEwController里添加两个VIEw的指针,不定义IBOutlet,同时定义方法switchVIEws #import <UIKit/UIKit.h> @class YellowVIEwController; @class BlueVIEwController; @interface SwitchVIEwController : UIVIEwController { } @property (retain,nonatomic) YellowVIEwController *yellowVIEwController; @property (retain,nonatomic) BlueVIEwController *blueVIEwController; - (IBAction)switchVIEws:(ID)sender; @end 7.代码架构好了,开始在IB中 *** 作 7.1 在library中拖动一个VIEw Controller到Window上面 7.2 该VIEw是UIVIEwController,在IDentity Inspector中修改类名为UIVIEwController 7.3 新建Toolbar的VIEw,拖放一个VIEw到7.1添加的VIEw上面,替换原有的VIEw(注:为何是替换呢?) 7.4 拖动一个Toolbar到7.3新建的VIEw中,选中Toolbar后,点击button,链接到SwitchVIEwController的方法switchVIEw中 8. 在IB中将AppDelegate中的IBOutlet switchVIEwController与类SwitchVIEwController链接 9. 修改SwitchVIEwController.m 主要就是从Nib加载VIEwController,不用的释放Controller 刚开始只加载BlueVIEw,因为YellowVIEw可能用户不会选择,等到切换的时候才加载(Lazy Loading) #import "SwitchVIEwController.h" #import "YellowVIEwController.h" #import "BlueVIEwController.h" @implementation SwitchVIEwController @synthesize yellowVIEwController; @synthesize blueVIEwController; - (voID)vIEwDIDLoad { BlueVIEwController *blueController = [[BlueVIEwController alloc] initWithNibname:@"BlueVIEw" bundle:nil]; self.blueVIEwController = blueController; [self.vIEw insertSubvIEw:blueController.vIEw atIndex:0]; [blueController release]; [super vIEwDIDLoad]; } - (IBAction)switchVIEws:(ID)sender { if (self.yellowVIEwController.vIEw.supervIEw == nil) { if (self.yellowVIEwController == nil) { YellowVIEwController *yellowController = [[YellowVIEwController alloc] initWithNibname:@"YellowVIEw" bundle:nil]; self.yellowVIEwController = yellowController; [yellowController release]; } [blueVIEwController.vIEw removeFromSupervIEw]; [self.vIEw insertSubvIEw:yellowVIEwController.vIEw atIndex:0]; } else { if (self.blueVIEwController == nil) { BlueVIEwController *blueController = [[BlueVIEwController alloc] initWithNibname:@"BlueVIEw" bundle:nil]; self.blueVIEwController = blueController; [blueController release]; } [yellowVIEwController.vIEw removeFromSupervIEw]; [self.vIEw insertSubvIEw:blueVIEwController.vIEw atIndex:0]; } } Releases the vIEw if it doesn't have a supervIEw - (voID)dIDReceiveMemoryWarning { // Releases the vIEw if it doesn't have a supervIEw [super dIDReceiveMemoryWarning]; // Release any cached data,images,etc,that aren't in use if (self.blueVIEwController.vIEw.supervIEw == nil) self.blueVIEwController = nil; else self.yellowVIEwController = nil; } - (voID)dealloc { [yellowVIEwController release]; [blueVIEwController release]; [super dealloc]; } @end 10.完善两个内容视图,添加button,d出不同的内容提示. #import <UIKit/UIKit.h> @interface BlueVIEwController : UIVIEwController { } - (IBAction)bluebuttonpressed; @end 在实现中加入UIAlertVIEw即可.(略去) 打开BlueVIEw.nib,在Indentity Inspector中选择class name为BlueVIEwController,表明从nib从BlueVIEwController 然后修改VIEw的背景色,更改VIEw的位置在属性页的 Simulated User Interface Elements选项下 11.加入视图切换动画 - (IBAction)switchVIEws:(ID)sender { [UIVIEw beginAnimations:@"VIEw Flip" context:nil]; [UIVIEw setAnimationDuration:1.25]; [UIVIEw setAnimationCurve:UIVIEwAnimationCurveEaseInOut]; ..... if(..) { [UIVIEw setAnimationTransition: uiviewanimationtransitionFlipFromright forVIEw:self.vIEw cache:YES]; [blueVIEwController vIEwWillAppear:YES]; [yellowVIEwController vIEwWilldisappear:YES]; … … } [UIVIEw commitAnimations]; } ---------------------我可以 *** 纵冰冷的代码,但我 *** 纵不了我的人生...... |
评论列表(0条)