本文介绍了当视图属性没有设置时,loadView多次调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚遇到了一些我以前没有见过的东西,我想我会在这里要求验证原因。我有一个UIController的视图是定义程序,我注意到,当我评论的代码出了它的loadView(测试其他),loadView和viewDidLoad都被调用了四次。我的想法是,因为我没有设置视图属性iOS是尝试多次加载视图,虽然我还是有点好奇,如果这是为什么viewDidLoad也被调用的情况。

I just came across something that I have not seen before and I thought I would ask here to verify the cause. I have a UIController who's view is defined programatically, I noticed today that when I commented the code out of its loadView (to test something else) that both loadView and viewDidLoad were both called four times. My thinking is that because I have not set the view property iOS is trying multiple times to load the view, although I am still a little curious if this is the case why viewDidLoad was also called.

- (void)loadView {
    NSLog(@"%s", __PRETTY_FUNCTION__);

    // MAP VIEW
    // MKMapView *tempMapView = [[MKMapView alloc] init];
    // [tempMapView setDelegate:self];
    // [self setView:tempMapView];
    // [self setCustomMapView:tempMapView];
    // [tempMapView release];
}

控制台输出:

2011-02-02 14:10:00.194 Xrails[19501:307] -[MapController loadView]
2011-02-02 14:10:00.209 Xrails[19501:307] -[MapController viewDidLoad]

2011-02-02 14:10:00.212 Xrails[19501:307] -[MapController loadView]
2011-02-02 14:10:00.226 Xrails[19501:307] -[MapController viewDidLoad]

2011-02-02 14:10:00.229 Xrails[19501:307] -[MapController loadView]
2011-02-02 14:10:00.243 Xrails[19501:307] -[MapController viewDidLoad]

2011-02-02 14:10:00.246 Xrails[19501:307] -[MapController loadView]
2011-02-02 14:10:00.259 Xrails[19501:307] -[MapController viewDidLoad]


推荐答案

我想这里有两点。首先,我认为你的假设是正确的,无论是要求视图是回到零,因此再次请求视图控制器的视图。这会导致它在文档。

I think there's two points here. Firstly, I think your assumption is right in that whatever is calling for the view is getting nil back and so asks for the view controller's view again. This causes it to go through -loadView as documented for -view in the documentation for UIViewController.

为什么 -viewDidLoad 后来被调用,即使它没有加载视图;我想象在 -loadView 被调用时,假设视图已经被加载并调用 -viewDidLoad

As for why -viewDidLoad is being called afterwards, even though it hasn't loaded the view; I imagine that where -loadView is being called it is assumed that the view has been loaded and calls -viewDidLoad straight afterwards.

无论如何,如果你自己实现 -loadView ,你必须有一个有效的视图。

Either way, if you implement -loadView yourself, you must have a valid view at the end of it.

这篇关于当视图属性没有设置时,loadView多次调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 06:54