Infinite loop when using loadView

A very interesting problem when using loadView in the UIViewController.

Usually we used this method

// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
    NSLog(@"loadview");
    [super loadView];
}

If remove

 [super loadView];

We get a dead loop with this

- (void)loadView {
    NSLog(@"loadview");

}

Why?

+5
source share
3 answers

Since you simply DIRECT what is implemented in the superclass (UIViewController), if you do not call the super methods, then the execution that needs to be done is NOT executed.

- - , , , ( , , , ) ADD local .

, , , , , .

, , super loadView - , .

: , , : http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html , , view .

+4

- . ():

- (void)loadView {
   self.view = [[UIView alloc] initWithFrame:self.view.bounds];
}

,

- (void)loadView {
   self.view = [[UIView alloc] initWithFrame:CGRectZero];
}

.

, , .

+9

loadView, . , loadView , , . :

, loadView .

, :

- (void)loadView {
    NSLog(@"loadview");
}

... self.view nil loadView

- (void)loadView {
   self.view; // Or anything that references self.view
}

... self.view loadView , , .

:

- (void)loadView {
    self.view = [[UIView alloc] init];
    if (self.view == nil) {
        [super loadView]; // Try to load from NIB and fail properly, also avoiding inf. loop.
    }
}
+2

All Articles