简体   繁体   中英

On iOS, how do applicationDidBecomeActive, loadView, and viewDidLoad get called?

The methods applicationDidBecomeActive , loadView , and viewDidLoad will get called at appropriate times in an iOS app. For loadView and viewDidLoad , it looks like it is:

-(void) someMethod {

    //...

    [viewController loadView];
    [viewController viewDidLoad];

}

Is that how they get called and what is the class that call them? (Is there source code that can show the flow of the starting of an app? A lot of times, we can only see the header files but not the source code).

If I understood well your question, you would like to know about the application lifecycle, is it true?

Well, I guess there is no source code provided by apple that can display you how it looks like.

If you want to know how happens when an application starts, I suggest to read about app-launch-sequence-ios-revisited by Oleb . It's a very good post.

About the methods you wrote, these methods shouldn't not called manually. It's the framework (through the iOS) that calls them for you.

The methods loadView and viewDidLoad are methods that are called during the UIViewController lifecycle.

You use (override) loadView when you cannot create a storyboard or a nib file. In this manner you can provide to your UIViewController a fresh view. From Apple doc:

If you cannot define your views in a storyboard or a nib file, override the loadView method to manually instantiate a view hierarchy and assign it to the view property.

In other words:

- (void)loadView
{  
   UIView* myCustomView = ... // create the view here
   self.view = myCustomView;
}

About the viewDidLoad method, this is called when a view has been set up in memory. Once done you are sure that outlets, for example, are set up and you can perform additional initializations.

From Apple doc:

This method is called after the view controller has loaded its view hierarchy into memory. This method is called regardless of whether the view hierarchy was loaded from a nib file or created programmatically in the loadView method. You usually override this method to perform additional initialization on views that were loaded from nib files.

In other words:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // additional initializations here
}

Finally, about applicationDidBecomeActive method (or delegate if you want), this is called to let your application know that it moved from the inactive to active state.

I suggest you to read UIApplicationDelegate and UIViewController class references.

If you want to simply verify the sequence call, override the methods and put a NSLog there.

Hope it helps.

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