ViewController编程指南(预备)

预备知识:

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,那么程序陷入死循环.












  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值