从手动构建window-base模板,看多视图中Window、AppDelegate、ViewController关系
先在Xcode4.2 中构建一个window-base模板,再从IB的连线过程中 整理三者的联系
还是使用上次的toolBar的思路,一个switchViewController当做rootViewController,另外随便两个ViewController负责输出两个子视图
一:连线之前的部分
1.首先创建“Empty Application”模板
此时项目中只有AppDelegate.h 和 AppDelegate.m,没有ViewController也没有MainWindow.xib
2.创建三个“UIViewController subclass”,根视图不需要xib,子视图需要xib
switchVC(ViewController)当做rootViewController,无需xib
firstVC和secondVC是子视图,创建时点上“需要xib”的复选框
3.创建一个UIwindow
4.在Window.xib里,加入一个Object实例和一个ViewController实例
5.还是在Window.xib里
File‘s Owner的Calss信息改成:UIApplication (为了实现委托)
Object实例的Class信息改成:AppDelegate
ViewController实例的Class信息改成:switchVC
二:开始连线
进入Window.xib的视图
1.
File’s Owner的输出“delegate” 拖动到 AppDelegate对象上
2.
AppDelegate对象的输出“window” 拖动到 Window对象上
3.
AppDelegate对象的输出“swVC”(switchVC的实例变量的输出) 拖动到 SwitchVC对象上
4.
项目的MainInterface改成window.xib
三:必须的代码
/*****AppDelegate.h*****/
{
switchVC* swVC;
}
@property (strong, nonatomic) IBOutlet UIWindow *window;
@property (retain,nonatomic) IBOutlet switchVC* swVC; //=>连线第三步的输出口
/*****AppDelegate.m*****/
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self.window addSubview:swVC.view]; //=>常用的添加子视图的方法
[self.window makeKeyAndVisible];
return YES;
}
/*****switchVC.h*****/
{
firstVC* first;
secondVC* second;
}
@property (retain,nonatomic)IBOutlet firstVC* first; //=>所持有的另外两个viewController
@property (retain,nonatomic)IBOutlet secondVC* second;
/*****switchVC.m*****/
- (void)viewDidLoad
{
firstVC* f = [[firstVCalloc]initWithNibName:@"firstVC"bundle:nil]; //=>初始化子视图
self.first = f; //=>让指针变量去retain
[f release]; //=>释放掉,已经没用
[self.view insertSubview:first.viewatIndex:0]; //=>插入子视图
[superviewDidLoad];
// Do any additional setup after loading the view from its nib.
}
这样就能显示第一个子视图了
switchView按钮的实现方法和昨天的代码一样,就不赘述了