Search code examples
iosloadview

Infinite loop when using loadView


Very interesting problem when using loadView in UIViewController.

Usually we used like this way

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

If remove

 [super loadView];

We will get dead loop with this

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

}

Why ?


Solution

  • Since you are just INHERITING what's being implemented in super class(UIViewController), if you don't call super methods, then implementation that needs to be done is NOT done.

    Almost all super methods do something critical and the local class inheriting super class implementations must either override them all together (unless you know everything about what super does by referring to the documentation, it's never a good idea), or just ADD local class implementation to the inherited super class implementations.

    In conclusion, whenever you inherit a class, which is in most cases of software development, you should let the super class do its implementations unless it's safe to override them.

    If I am correct, it seems like super loadView implements something very critical to avoid the loop.

    ADDITIONAL NOTE: However, based on the documentation, you should not call super method: http://developer.apple.com/library/ios/#documentation/uikit/reference/UIViewController_Class/Reference/Reference.html Probably, the reason for infinite loop is caused by not implementing view property appropriately.