Reputation: 128
I create my views programmatically. If I do not put loadView
method, the app runs well. However, When I add loadView
method like this:
- (void)loadView
{
NSLog(@"loadView is called");
}
I found this method was called many times! At last, the app crashed.
I wonder why loadView
method is called so many time.
Can anyone help? Thanks a lot!
Upvotes: 1
Views: 1091
Reputation: 119242
loadView
is expected to, at some point, populate the view
property of a view controller. The view property is lazily loaded (look at the call stack, you will see a method called something like _loadViewIfNeeded
).
If loadView
doesn't create a view, then each time the .view
property is accessed, the view controller will call loadView
again, trying to lazily load the view. At some point everything will go wrong because a view controller needs a view. If you access self.view from within your custom loadView, you'll get an infinite loop.
From the documentation:
You can override this method in order to create your views manually. If you choose to do so, assign the root view of your view hierarchy to the view property. The views you create should be unique instances and should not be shared with any other view controller object. Your custom implementation of this method should not call super.
Upvotes: 7
Reputation: 884
In your load view are you calling [self loadView]
rather than [super loadView]
Upvotes: 0