Analyze the call sequence and call sequence of all methods when the application starts.

Source: Internet
Author: User

Analyze the call sequence and call sequence of all methods when the application starts.

The Startup Process of an application should include the creation of a proxy, the loading of the controller, and the loading of the controller view. There are many methods about the lifecycle, and each method is sequential, if the call sequence is inaccurate or the method for writing a code segment is inappropriate, various strange issues may occur. In this article, _ FUNCTION _ printing is used in almost all methods to be called at startup. There are some unexpected results.

If you did not see this article in Dong Boran's blog, click to view the original article.

First, review the application startup process.

①. Load the Main function first

②. In the UIApplicationMain method of the Main function, create the Application object and create the Delegate object of the Application.

③ Create a main loop and the proxy object starts to listen to events

④. After the startup is complete, the didFinishLaunching method is called and a UIWindow is created in this method.

⑤ Set the root controller of UIWindow

6. If a storyboard exists, the storyboard entry of the application is found in info. plist and the Controller pointed by the arrow is loaded.

7. display window


The method to be called from step 3 to Step 7

There are 18 methods for AppDelegate, ViewController, MainView (Controller View), and ChildView (Child control View ).

In AppDelegate:

1. application: didfinishlaunchingwitexceptions:

2. applicationDidBecomeActive:


In ViewController:

3. loadView

4. viewDidLoad

5. load

6. initialize

7. viewWillAppear

8. viewWillLayoutSubviews

9. viewDidLayoutSubviews

10. viewDidAppear


In MainView (Controller View:

11. initWithCoder (if there is no storyboard, initWithFrame will be called. Here two methods are considered as one)

12. awakeFromNib

13. layoutSubviews

14. drawRect


In ChildView (Child control View:

15. initWithCoder (if there is no storyboard, initWithFrame will be called. Here two methods are considered as one)

16. awakeFromNib

17. layoutSubviews

18. drawRect


So the question is, can you sort the 18 methods in the order below?



The following figure shows the beta2 version of Xcode6.3.

Sometimes there are changes, that is, the last two methods are somewhat different.


I prefer Xcode 6.1. I think it is more scientific. The following is how to sort out the methods.


+ (void)load;

1. This is the method that will be called when the application starts. The code written in this method is called first (Dong baoran is original)


+ (void)initialize;

2. This method is called only when this class is used. In this method, the topic of the navigation controller is usually set. If you set the topic of the navigation bar in the following method, it will be too late! (Of course, the above method can also be written)


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions;

3. In this method, a UIWindow is created, and the root controller is set and displayed. For example, some applications add the authorization page to load the authorization page. You can also set the observer to listen to the notification to switch to the root controller.


ChildView - (instancetype)initWithCoder:(NSCoder *)aDecoder; 

4. I never expected that childView's initwithcoder would be called before MainView's method. If none of the parent's methods come out, the Child control should be completed first? Thank you for your understanding.


MainView - (instancetype)initWithCoder:(NSCoder *)aDecoder;

5. It refers to the archive operation after data storage of applications.


MainView - (void)awakeFromNib;

6. in this method, set the view background and a series of common operations. Do not write the frame information. The use of this method will be involved only when IB is used. when the nib file is loaded, an awakeFromNib message is sent. every object in the nib file, each object can define its own awakeFromNib function to respond to this message and perform some necessary operations.


ChildView - (void)awakeFromNib

7. The child control also has this method to override the method of the parent class. The basic usage is the same as above.


- (void)loadView;

8. Create a view hierarchy. Note that you cannot directly write self. view without creating a view for the Controller. Because the underlying layer of self. view is:

If (_ view = nil ){

_ View = [self loadView]


Therefore, such writing will directly cause an endless loop.

If you rewrite this loadView method without writing anything, a black screen is displayed.

If [super view] is written, it also depends on the initWithNibName (xib name specified) written by the previous controller during creation, or the normal init written. For the latter or black screen.

If it is not in this method, the bottom layer of init will call initWithNibName. If the name is MainViewController, MainView. xib cannot be found in the project, and MainViewController. xib will be found again.


- (void)viewDidLoad;

9. this method is the most widely used method in the past, but it will become increasingly unreliable in subsequent development. Many things have not been loaded yet, and various values are not accurate, I seldom write anything here. The view component is loaded. Do not set attributes such as frame before the layout is started! Sometimes there may be 20 pixels in the gap.


- (void)viewWillAppear:(BOOL)animated;

10. The view will appear. This method is often used. For example, if you want to set the setNavigationBarHiden: animate: in the navigation bar, you must write it here to perfectly fit it. There are many more, such as monitoring screen rotation,

ViewWillTransitionToSize: it may need to be adjusted again in this method, or reloadData or automatic pull-down refresh on the new interface will be written in this method.


- (void)viewWillLayoutSubviews;

11. the view is about to deploy the subview. Apple suggested the method for setting the interface layout attribute. In this method, no code is written at the underlying layer of the system in viewWillAppear. That is to say, super is also acceptable.


MainView  - (void)layoutSubviews;

12. in this method, the frame of the sub-control is usually set, because the layout is basically complete, and the frame or self obtained during the settings. bounds is the most accurate. If it is written in awakeFromeNib, it will be inaccurate. Also, remember not to forget super layoutSubviews. It may be hard to find this bug at the end.


- (void)viewDidLayoutSubviews;

13. I did not expect this method. The Controller's view sub-control has not been well laid out yet. How can this controller already say that the layout is complete? What about the layout behind? If you have some insights, please tell me what Apple means.


ChildView - (void)layoutSubviews;

14. The layout of the child controls in the subcontrols of the controller is written here.


MainView - (void)drawRect:(CGRect)rect;

15. by default, all the UI controls are painted. In this step, you can draw everything. Sometimes you need to use Quartz2D knowledge in this method, however, do not forget to write "super". Otherwise, the system won't be able to draw any original items. We recommend that you use the besell path to draw images as much as possible, because the default context method of the system may sometimes cause memory leakage. The drawRect method can be called only once during loading. If you still need to call the method later, such as downloading the progress arc, you must use setNeedsDisplay to call this method multiple times at regular intervals.


ChildView - (void)drawRect:(CGRect)rect;

16. The internal Drawing Method of the Child control of the view, sometimes you can customize the middle of the label with a strikethrough (used to write the original price before the discount) is to draw the root line here.


- (void)viewDidAppear:(BOOL)animated;

17. After drawing all the above images, the view is fully loaded. The operation here may be to set some animations on the page, or to set code operations such as tableView, collectionView, and QQ chat pages to scroll to the bottom of scrollToIndexPath.


- (void)applicationDidBecomeActive:(UIApplication *)application;

18. In the end, this is the focus Method for the application of AppDelegate to get the focus. In this case, all things are loaded, and the application is fully loaded and remains in the best status for user operations. In this method, a class will write about the pop-up keyboard method. For example, some user logon interfaces allow you to log on to the logon interface when you just open the program, the focus of the cursor will automatically flash in the account text box, that is, set the account text box as the first responder. The keyboard pops up from the bottom after the page is loaded. This code is generally written in this method.


If you did not see this article in Dong Boran's blog, click to view the original article.

In the above summary, we would like to share some comments with you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.