loadView and viewDidLoad

loadView and viewDidLoad

loadView
Creates the view that the controller manages.

- (void)loadView
Discussion
You should never call this method directly. The view controller calls this method when its view property is requested but is currently nil. This method loads or creates a view and assigns it to the view property.

If the view controller has an associated nib file, this method loads the view from the nib file. A view controller has an associated nib file if the nibName property returns a non-nil value, which occurs if the view controller was instantiated from a storyboard, if you explicitly assigned it a nib file using the initWithNibName:bundle: method, or if iOS finds a nib file in the app bundle with a name based on the view controller’s class name. If the view controller does not have an associated nib file, this method creates a plain UIView object instead.

If you use Interface Builder to create your views and initialize the view controller, you must not override this method.

You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property. 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.

If you want to perform any additional initialization of your views, do so in the viewDidLoad method. You should also override the viewDidUnload method to release any references to the view or its contents.(最后一句在iOS6的文档中没有了,因为在iOS6中已经抛弃viewDidUnLoad方法)

你不应该直接调用该方法。当其 view property 被需要且为nil时,view controller 将会调用该方法。该方法载入或创建一个视图并赋值给 view property。

如果 view controller 有一个相关联的nib文件,该方法将从nib文件中载入view。一个 view controller 有一个相关联的 nib 文件,如果view controller 已经从一个 storyboard 实例化,或你明确地使用 initWithNibName:bundle: 方法赋值给 nibName 或者 iOS 在 app bundle 依据 view controller 的类名找到一个 nib 文件,其 nibName property 将返回一个非空的值。如果 view controller 没有一个相关联的 nib 文件,该方法创建一个 plain UIView 对象代替。

如果你用 IB 创建你的视图并初始化 view controller ,你必须不重载该方法。

你可以重载该方法去创建你的视图。如果你选择了这么做,赋值给你的视图层次中的根视图的 view property。你创建的视图应该是唯一的实例,而且不应该和其他 view controller 对象共享。你对该方法的实现不应该调用其父方法。

如果你想做一些额外的关于视图的初始化,请在 viewDidLoad 方法中执行。你应该总是重载 viewDidUnload 方法 用来 release 所有对视图或其内容的引用。

UIViewController 中的 loadView 和 viewDidLoad
// Creates the view that the controller manages.
- (void)loadView
{
    // 若重载了该方法,则 self.isViewLoaded 返回值为NO
    if (self.isViewLoaded)
        NSLog(@"\nloadView self.isViewLoaded YES");
}

// Called after the controller’s view is loaded into memory.
- (void)viewDidLoad
{
    if (self.isViewLoaded)
        NSLog(@"\nviewDidLoad self.isViewLoaded YES");
}

在两个方法中分别设置断点,做两次测试。
第一次测试重载两个方法,当程序停在断点处时,可以看到堆栈中的前一个为 [UIViewController view] ,调用 self.isViewDidLoad 返回 NO 。继续运行,程序停在 viewDidLoad 方法的断点处时,其堆栈中的前一个仍然是 [UIViewController view] ,调用 self.isViewDidLoad 还是返回 NO 。应该是 view controller 中的 view 合成器方法中调用了 loadView 和 viewDidLoad ,view 也没被加载。

第二次测试只重载 viewDidLoad 。程序停在断点处时,堆栈中的前一个仍为 [UIViewController view] ,但是这次,调用 self.isViewDidLoad 返回 YES ,即 view 被加载了。

总结:
1、在同一个 UIViewController 中,若同时重载了 loadView 和 viewDidLoad 两个方法,则会先调用 loadView,之后调用 viewDidLoad。在两个方法中调用 self.isViewDidLoad 都会返回 NO , 而且self.view 为nil。根据 loadView 文档中的描述,当需要使用 self.view ,而 self.view 为 nil 时,view controller 将会调用loadView,所以会造成死循环。(The view controller calls this method when its view property is requested but is currently nil.)
2、若只重载 viewDidLoad ,self.isViewDidLoad 返回 YES ,而且 self.view 不为空。
所以若是需要手动创建 view ,可以重载 loadView 方法,并创建一个 view 对象赋值给 self.view,或者补充在 loadView 方法,只重载 viewDidLoad 方法,self.view 将会被加载,不为空。

ps:若重载了 loadView 方法,则只要 self.view 为nil,必会造成死循环(当需要使用 self.view ,而 self.view 为 nil 时,view controller 将会调用loadView,所以会造成死循环。)
对于 view 属性,文档中有一句如是说:The UIViewController class can automatically set this property to nil during low-memory conditions and also when the view controller itself is finally released. 故在 viewDidLoad 中对一些对象做初始化的话,可能会造成内存泄露

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值