iPhone开发学习笔记007——Xcode4.2下iPhone多视图开发(自已创建MainWindow.xib和不用MainWindow.xib两种实现)
使用Xcode4以前的版本进行iPhone开发,新建Window-based Application类型的工程时,都会自动生成MainWindow.xib,并且都至少包含一个UIApplicationDelegate和一个UIWindow对象。
但是Xcode4以后,新建工程时,工程template这一块有很大改动,没有Window-based Application这一项了,但是有个Empty Application(iOS->Application下),现在描述以这种方式新建工程,然后自已创建MainWindow.xib并使用,和不用MainWindow.xib两种实现。
另外多视图实现采用三个UIViewController来实现,一个FirstViewController使用FirstView.xib设计界面,上面一个按钮点后切找到SecondViewController的界面,一个SecondViewController使用SecondView.xib设计界面,上面也有一个按钮点后切换到FirstViewController,一个SwitchViewController负责这两个controller的界面的切换,SwitchViewController作为rootViewController,如果一个应用没有rootViewController启动就会报错“Applications are expected to have a root view controller at the end of application launch”,其实上面两种方式就是:
第一种:创建MainWindow.xib,默认有window,需要手动添加delegate指定所属类为MVTestAppDelegate,添加UIViewController,指定所属类为SwitchViewController,然后在委托MVTestAppDelegate代码中[windowaddSubview:viewController.view];将该controller的view添加到window。不需要再使用代码来创建和初始化应用委托的window和controller的view了,这些都通过MainWindow.xib来做了。下面第二种方式就是这部分不通过MainWindow.xib来做,而是通过代码实现就用delegate的window和controller的创建和初始化
第二种:即上面第一种方式中MainWindow.xib实现的工作通过Delegate中代码来实现。详细如下:
CMD+SHIFT+N打开新建工程窗口,选择Empty Application这一项(会默认提供一个delegate和一个window),取名为MVTest。如下图:
新建SwitchViewController继承UIViewController,去掉with XIB for user inte易做图ce,
同SwitchViewController一样,新建FirstViewController,和SecondViewController,然后新建FirstView.xib和Second.xib,如下图:
两个xib上面分别都添加一个label和一个button,如下图:
接下来在MVTestAppDelegate头文件中添加:
@property (nonatomic, retain)SwitchViewController *viewController;// 注意:window和viewController前面的IBOutlet
+(MVTestAppDelegate *)App;
并且在*.m文件中实现,如下图:
另外,SwitchViewController.h:
#import <UIKit/UIKit.h>
@classFirstViewController;
@classSecondViewController;
@inte易做图ce SwitchViewController :UIViewController {
FirstViewController* firstviewcontroller;
SecondViewController* secondviewcontroller;
}
@property (nonatomic,retain)FirstViewController* firstviewcontroller;
@property (nonatomic,retain)SecondViewController* secondviewcontroller;
-(void)initView;
-(void)showFirstView;
-(void)showSecondView;
-(void)removeAllView;
@end
SwitchViewController.m:
#import "SwitchViewController.h"
#import "FirstViewController.h"
#import "SecondViewController.h"
@implementation SwitchViewController
@synthesize firstviewcontroller;
@synthesize secondviewcontroller;
-(void)initView{
NSLog(@"ttt");
if(self.firstviewcontroller ==nil){
self.firstviewcontroller = [[FirstViewControlleralloc]initWithNibName:@"FirstView"bundle:nil];
}
[selfremoveAllView];
[self.viewinsertSubview:self.firstviewcontroller.viewatIndex:0];
}
-(void)showFirstView{
if(self.firstviewcontroller ==nil){
self.firstviewcontroller = [[FirstViewControlleralloc]initWithNibName:@"FirstView"bundle:nil];
}
[selfremoveAllView];
[self.viewinsertSubview:self.firstviewcontroller.viewatIndex:0];
}
-(void)showSecondView{
if(self.secondviewcontroller ==nil){
self.secondviewcontroller = [[SecondViewControlleralloc]initWithNibName:@"SecondView"bundle:nil];
}
[selfremoveAllView];
[self.viewinsertSubview:self.secondviewcontroller.viewatIndex:0];
}
-(void)removeAllView{
for(NSInteger i=0;i<[self.view.subviewscount];i++){
[[self.view.subviewsobjectAtIndex:i]removeFromSuperview];
}
}
@end
在这两个controller,FirstViewController和SecondViewController里面,均分别添加-(IBAction)buttonClick:(id)sender;,并实现:
-(IBAction)buttonClick:(id)sender{
[[MVTestAppDelegateApp].viewControllershowSecondView]; //FirstViewController调用这个showSecondView,SecondViewController里面调用showFirstView.
}
然后分别指定FirstView.xib的File's Owner所属类为FirstViewController,SecondView.xib的所属类为SecondViewController,并将xib界面上的按钮与IBAction连接,同时将Outlets下面的view与界面的view连接。如下图:
接下来涉及到MainWindow.xib了:
一、不用MainWindow.xib
(1) 初始化delegate的window.
self.window = [[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]]autorelease];
(2) 指定delegate所在window的rootViewController,即SwitchViewController,需要新建并初始化.
self.viewController = [[[SwitchViewControlleralloc]init]autorelease];
[self.viewControllerinitView];
self.window.rootViewController =self.viewController;
如下图:
二、自已创建MainWindow.xib并使用
新建一个window类型的xib命名为MainWindow.xib。如下图:
,新建好后,从Library中拖一个Object到界面上,指定其所属类为MVTestAppDelegate,
然后拖一个View Controller到界面上,并且指定所属类为SwitchViewController。
指定MainWindow.xib的File's Owner的所属类为UIApplication,并将其delegate与MVTestAppDelegate相连接:
接下来选中Test App Delegate,即MVTestAppDelegate,将viewController与界面上的Switch View Controller相连,window与window相连,
补充:移动开发 , IOS ,