Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loadView called multiple times when view property not set?

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];
}

Console output:

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]
like image 314
fuzzygoat Avatar asked Feb 02 '11 14:02

fuzzygoat


3 Answers

Add [super loadView] at the beginning of the loadView method

like image 54
Asad Khan Avatar answered Nov 15 '22 02:11

Asad Khan


Put your code in viewDidLoad, and comment loadView method

like image 29
iHS Avatar answered Nov 15 '22 02:11

iHS


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.

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.

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

like image 27
Daniel Tull Avatar answered Nov 15 '22 03:11

Daniel Tull