简体   繁体   中英

loadview method is called several times

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!

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.

在加载视图中,您要调用[self loadView]而不是[super loadView]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM