如果使用代码创建view,那么就需要重写loadView方法:
在这个方法中,如果不创建view,就会循环的调用loadView.
- (void)loadView
{
UIView *view = [[UIViewalloc]initWithFrame:[[UIScreenmainScreen]applicationFrame]];
self.view = view;
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeRoundedRect];
button.frame =CGRectMake(100,106,132,132);
[button addTarget:selfaction:@selector(buttonPressed:)forControlEvents:UIControlEventTouchUpInside];
// button.userInteractionEnabled = YES;
[button setTitle:@"A"forState:UIControlStateNormal];
[self.viewaddSubview:button];
}
在这个方法中必须创建视图控制的view,也就是self.view。
或者调用[super loadView]方法也是可以的。
如果是下面这样:
- (void)loadView
{
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
button.frame = CGRectMake(100,106,132,132);
[button addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
// button.userInteractionEnabled = YES;
[button setTitle:@"A" forState:UIControlStateNormal];
[self.view addSubview:button];
}
就会死掉,一直循环的调用loadView,因为视图控制器的view没有创建。
视图装载的过程
很清晰的从这张图片看出来,走到is there a view的时候,因为一直没有创建view,那么就会循环的调用loadView。
视图的卸载过程