视图的生命周期其实可以理解为Load-Present-Hidden(加载-展现-隐藏)三个阶段,如果从ViewController中方法中执行的顺序来看,顺序应该是这样的:
loadView→viewDidLoad→viewWillAppear→viewDidAppear→viewWillDisappear→viewDidDisappear→dealloc
loadView:一般情况下不用用到,除非需要重写设置View;
viewDidLoad/dealloc:视图加载完成之后的设置和视图销毁的时候调用;
viewWillAppear/viewWillDisappear:视图即将呈现和视图即将消失;
viewDidAppear/viewDidDisappear:视图展现在屏幕的时候和视图完全消失在屏幕的时候调用,默认不做任何操作.
LifeCircle- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"viewDidLoad-keso");
}
-(void)viewWillAppear:(BOOL)animated{
NSLog(@"viewWillAppear-FlyElephant");
}
-(void)viewDidAppear:(BOOL)animated{
NSLog(@"viewDidAppear");
}
-(void)viewWillDisappear:(BOOL)animated{
NSLog(@"viewWillDisappear");
}
-(void)viewDidDisappear:(BOOL)animated{
NSLog(@"viewDidDisappear-http://www.cnblogs.com/xiaofeixiang/");
}
viewWillLayoutSubviews和viewDidLayoutSubviews如果使用到自动布局,需要修改自动布局中的约束条件,那么可以在viewWillLayoutSubviews中进行操作,最近遇到一个就是text文本框中文字过多的时候文字不能不能置顶,可以在viewWillDidSubviews中操作:
- (void)viewDidLayoutSubviews {
[self.myTextView setContentOffset:CGPointZero animated:NO];
}
网友评论