序
这几天在看些View Controler 相关知识,白胡子老爷爷讲的很有深度,也很浅显易懂.静下心来总结了一下关于一个View Controller 的Lifecycle
View Controller 的创建
MVCs are most often instantiated out of a storyboard (as you’ve seen)
They are ways to do it in code (rare) as well which we may cover later in quarter
我们的view controller 总是在storyboard 中创建,当然也有可能是在代码中只用代码进行创建
awakeFormNib()
当.nib文件被加载的时候,会发送一个awakeFromNib的消息到.nib文件中的每个对象,每个对象都可以定义自己的awakeFromNib()来响应这个消息,执行一些必要的操作。也就是说通过nib文件创建view对象时执行awakeFromNib
Preparation is being segued to
在我们在使用segue进行场景的跳转的时候,我们需要做的一些事情就是准备一些数据给将要展现出来的视图.需要注意的是这个时候千万去对下一个界面的UI进行一些操作,因为这个时候Outlet 还没有被设置.所以系统会报错.
所以我使用了一些简单的方法进行验证
使用了两个View Controller ,当我试图从第一个vc跳转到第二个vc的时候,调用的顺序是这样的
- (IBAction)Show:(id)sender {
[self performSegueWithIdentifier:@"showStep2" sender:sender];
}
- (void)performSegueWithIdentifier:(NSString *)identifier sender:(id)sender{
NSLog(@"performSegueWithIdentifier is called");
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSLog(@"prepareForSegue is called");
}
我用了一个按钮去触发转场,然后观察他们之间被调用的先后关系.因为我知道在prepareForSegue的时候,第二个vc的Outlet还没有被设置好,所以我们在进行转场的时候根本就不能去更新转场后的UI界面.这样程序会崩溃掉.因此我们可以传值,但是并不可以更新UI
Outlet setting
viewDidLoad
After instantiation and outlet-setting,ViewDidLoad is called
Onething you may well want to do here is update your UI from your model
Because now you know all of your outlets are set
什么时候我们的程序的Outlet设置好了呢,在viewDidload()的时候,这个时候我们需要做一些初始化的工作,而且这个时候我们的Outlet已经连接好了,这个时候我们就可以进行UI更新的一些操作了.比如说我用Segue传值到了第二个界面.这个时候在对这些数据进行一些处理,然后更新我们的UI.
But be careful because the geometry of your view(its bounds)is not set yet!
At this point , you can’t be sure you ‘re on an iPhone screen or iPad screen or ??
So do not initialize things that are geometry-dependent
这个时候我们view的bounds 还没设置好,它不能确定自己的位置在哪里.所以不要做一些和位置有关的操作.
在这个时候视图还没有展现在我们的面前,通过下面的断点可以看出来
Appearing and DisAppearing
在第二个vc中我重写了这些方法
- (void)viewWillAppear:(BOOL)animated{
NSLog(@"viewWillAppear is called");
}
- (void)viewDidAppear:(BOOL)animated{
NSLog(@"viewDidAppear is called");
}
在第二个vc中我重写了这两个方法
- (void)viewWillDisappear:(BOOL)animated{
NSLog(@"vc1 viewWillDisappear is called");
}
- (void)viewDidDisappear:(BOOL)animated{
NSLog(@"vc1 viewDidDisappear is called");
}
他的运行结果在断点一是这样的
这个时候我们的第一个界面还没有消失
接着程序继续执行到了断点二
这个时候我看到第二个界面已经展现出来了,这就说明我们的界面出现的时间和viewWillAppear viewDidAppear之间的关系是这样的
调用viewWillAppear->界面的展现 ->调用viewDidAppear
继续执行以后,我们的第二个界面已经展现出来了,通过控制台打印的信息我们很清晰的知道了这些方法的调用过程.这就是Appearing and DisAppearing
Just before your view appears on screen , you get notified
viewWillAppear could be right after viewDidLoad or it could be a long time after viewDidLoad.
It depends on what’s going on in your UI
Your view will only get “loaded”once, but it might appear and disappear a lot
So don’t put something in this method that really wants to be in viewDidLoad.
Otherwise,you might be doing something over and over unnecessarily.
Do something here if things your display is changing while your MVC is off-screen.
You could use this to optimize performance by waiting until this mrthods is called (as opposed to viewDidLoad) to kick off an expensive operation(probably inanother thread)
viewWillAppear中可以放一些你并不想让viewDidLoad处理的事情.而且这个时候你的View的bounds已经设置好了,可以做一些和位置有关的事情
Low-memory
didReceiveMemoryWarning
In low-Memory situation,didReceiveMemoryWarning gets Called
This rarely happens,but well-designed code with big-ticket memory uses might anticipate it
Anything "big" that is not currently in use and can be recreated relative easily
should probably be released(by setting any pointers to it to nil)
可以在上面做一些释放内存的操作,比如说声音和图像.这个时候他们就会被释放掉,因为系统提示你内存紧张了