1.loadView
1.1 如果重写了控制器的loadView方法,则控制器的View按照loadView方法的描述去创建
- (void)loadView
{
self.view = [[UIView alloc]init];
self.view.backgroundColor = [UIColor redColor];
}
1.2 如果没重写控制器的loadView方法,则看有没有storyboard的,有的话,则按storyboard的描述创建view
加载storyboard的方法:
1>在project--targets--MainInterface指定storyboard
2>代码加载
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
UIStoryboard *myStoryboard = [UIStoryboard storyboardWithName:@"myStoryboard" bundle:nil];
self.window.rootViewController = [myStoryboard instantiateInitialViewController];
[self.window makeKeyAndVisible];
return YES;
}
1.3 如果没有storyboard,则看初始化控制器的时候,有没有指定nibName,指定了的话,就按nibName对应的xib创建view
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];
self.window.rootViewController = [[QKViewController alloc]initWithNibName:@"myViewController" bundle:nil];
[self.window makeKeyAndVisible];
return YES;
}
1.4 如果没有指定nibName,假设控制器名字是QKViewController,则程序优先寻找名字为QKView.xib,找到了的话,则按QKView.xib创建view
1.5 如果没有找到QKView.xib,则寻找QKViewController.xib,找到了的话,则按QKViewController.xib创建view
前面5步的流程图:
2.前面5步实际都是在做loadView,做完这一步之后,就调用viewDidLoad,只会被调用一次
3.在视图显示之前调用viewWillAppear,该函数可以被多次调用
4.视图显示完毕,调用viewDidAppear
5.在视图消失之前,调用viewWillDisApperar,该函数可以被多次调用
6.在布局变化前后,调用viewWillLayoutSubviews\viewDidLayoutSubviews
7.控制器还有三个方法需要注意:viewWillUnload\viewDidUnload\didReceiveMemoryWarning
当程序内存不够用时,最先拿到内存警告的是UIApplication→Window→Window.rootViewController→.....(一层一层往下传).
控制器收到内存警告时,如果要确定要销毁View,则会调用viewWillUnload\viewDidUnload.
控制器是这样判断View是否需要销毁的: