Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Infinite loop when using loadView

Tags:

ios

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 ?

like image 620
Forrest Avatar asked Jan 28 '11 06:01

Forrest


2 Answers

Only one way to make infinite loop in this case - is getting view property until its not set. If you write next (for example):

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

You'll got infinite loop, but

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

works OK.

So you can't to access view property until you didn't set it.

like image 146
Jlexyc Avatar answered Oct 10 '22 20:10

Jlexyc


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.

like image 30
petershine Avatar answered Oct 10 '22 22:10

petershine