预备知识:
1:init 和 initWithNibName的关系:
不管有没有nib文件,用init方法去对viewController进行初始化时都会先执行initWithNibName然后再执行init…
但是如果用initWithNibName方法去进行初始化的时候,则不会执行init方法.
- (id)init
{
self = [super init];
if(self)
{
NSLog(@"init");
}
return self;
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nil];
if(self)
{
NSLog(@"initWithNibName");
}
return self;
}
2013-07-30 11:36:02.678 memoryDemo[1410:c07] initWithNibName
2013-07-30 11:36:02.679 memoryDemo[1410:c07] init
(PS:如果没有你没有复写loadView,【- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil 】方法中参数'nibNameOrNil'又传了nil,则
a、If the view controller class name ends with the word “Controller”, as in MyViewController, it looks for a nib file whose name matches the class name without the word “Controller”, as in MyView.nib.
即MyViewController会找除了Controller的MyView.nib去进行初始化
b、It looks for a nib file whose name matches the name of the view controller class. For example, if the class name is MyViewController, it looks for a MyViewController.nib file.
即MyViewController会找同名的文件MyViewController.nib去进行初始化)
2:关于loadView值得注意的是,)
1、if you use Interface Builder to create your views and initialize the view controller, you must not override this method.(xib和loadView只能选择一个)
2、The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super
.(如果选择使用loadView,那么复写loadView的时候一定不能调用[super loadView],这样来保证视图实例不被其他视图控制器控制)
3:其实从xib初始化和loadView初始化都是为了让视图控制器加载其视图实例
执行viewDidLoad时候,self.view如果还是nil,系统会自动调用那个loadView方法去继续加载视图,如果LoadView将self.view置为nil,那么程序陷入死循环.