Sequence analysis of calls to all methods at application startup

Source: Internet
Author: User

An application start-up process to include the creation of agents, controller loading and controller view load, there are many methods of life cycle, each method is sequential, if the call order is not in doubt, or a piece of code written method is not appropriate, you will encounter a variety of wonderful problems. This article is not afraid of trouble in almost all of the methods to be invoked at startup using __function__ printing. And there were some surprises.

If you do not see this article in the Dong Platinum Blog Park, please click to view the original

First review the application startup process

①. Load the main function first

②. In the Uiapplicationmain method in the main function, create a Application object to create the delegate object of the application

③. Creating a main loop, the proxy object starts listening for events

④. The Didfinishlaunching method is called when it is started and is created in this method UIWindow

⑤. Set UIWindow who is the root controller

⑥. If there is a storyboard, the application's entry storyboard is found in Info.plist and the controller that the arrow refers to is loaded

⑦. display window

This article considers the method that will be called after step 3 to the end of step 7

There are 18 methods of appdelegate,viewcontroller,mainview (view of the Controller), Childview (view of the child control)

In the Appdelegate:

1.application:didfinishlaunchingwithoptions:

2.applicationDidBecomeActive:

In the Viewcontroller:

3.loadView

4.viewDidLoad

5.load

6.initialize

7.viewWillAppear

8.viewWillLayoutSubviews

9.viewDidLayoutSubviews

10.viewDidAppear

MainView (view of the Controller):

11.initWithCoder (call initWithFrame if not storyboard, here are two methods as one)

12.awakeFromNib

13.layoutSubviews

14.drawRect

Childview (Child control view):

15.initWithCoder (call initWithFrame if not storyboard, here are two methods as one)

16.awakeFromNib

17.layoutSubviews

18.drawRect

So the question is, don't look down you can put the above 18 methods in order?

The figure below is the Beta2 version of Xcode6.3.

Sometimes there's a change, and the last two methods are different.

I prefer Xcode 6.1 to think more scientifically. The following is the collation of the various methods

+ (void) load;

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

+ (void) initialize;

2. This is required to use this class only call, this method is generally written to set the navigation controller theme Ah, and so on, if the following method set the navigation bar theme is late! (Of course it can be written in the above method.)

-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (Nsdictionary *) launchOptions;

3. This method will create UIWindow, set up the root controller and display, such as some applications to load the authorization page is also in this addition, you can set the observer, listen to the notification switch root controller

4. Here I never thought, Childview Initwithcoder will be in the Mainview method before the call, the father has not come out, the first whole child control? Have a more thorough understanding of Bo friends kindly tell me thank you.

MainView-(Instancetype) Initwithcoder: (Nscoder *) Adecoder;

5. It is the operation of the file after the data storage of the application.

MainView-(void) awakefromnib;

6. In this method set the background of the view, such as a series of ordinary operations, do not write about the frame is not allowed, when using the IB will be involved in the use of this method, When the. nib file is loaded, a awakefromnib message is sent to each object in the. nib file, and each object can define its own awakefromnib function to respond to the message and perform some necessary actions.

Childview-(void) awakefromnib

7. Child controls also have this method, overriding the methods of the parent class. Basic usage Ibid.

-(void) Loadview;

8. To create a hierarchy of views, it is important to note that Self.view cannot be written directly without creating a view of the controller because the bottom of Self.view is:

if (_view = = nil) {

_view = [Self Loadview]

}

So writing this will directly cause a dead loop.

If you rewrite this loadview method, it will show a black screen if nothing is written.

If you write [super view] also depends on the previous controller is written in the creation of Initwithnibname (named Xib name), or write the normal init. If it is the latter or the black screen.

If it is not in this method, the lower level of Init will call Initwithnibname, if the name is Mainviewcontroller, will first find in the project Mainview.xib find not to find mainviewcontroller.xib.

-(void) viewdidload;

9. Lying trough, this method is used the most methods, but in the development later will find more and more unreliable, a lot of things have not been loaded, a variety of values are not accurate, very few in the face of writing things. This is just loading the view element and not starting the layout. do not set properties like frame! In some cases, there may be a difference of 20 pixels.

-(void) Viewwillappear: (BOOL) animated;

10. The view will appear, this method is used very much, for example, if you want to set the navigation bar Setnavigationbarhiden:animate: You have to write here, in order to perfect fit, no card jumping. There's a lot more, like listening to the screen spinning.

Viewwilltransitiontosize: This method may need to be adjusted again, or the new to the interface to Reloaddata or automatic drop-down refresh is written in this method.

-(void) viewwilllayoutsubviews;

11. The view will be layout sub-view, Apple proposed to set the interface layout properties of the method, this method and Viewwillappear, the system is not write any code at the bottom, that is, it is not possible to write super.

MainView  -(void) layoutsubviews;

12. In this method is generally set the sub-control frame, because this is equivalent to the layout is basically completed, set the frame or the self.bounds is the most accurate, if in the awakefromenib write will be inaccurate. And here to remember that you must not forget the super layoutsubviews, it may be difficult to find this bug in the end

-(void) viewdidlayoutsubviews;

13. This method I also have to play to play unexpectedly, the controller's view of the child control has not been laid out, how the controller has said that the layout is complete? What about the layout behind it? I implore you to tell me what the meaning of apples is.

Childview-(void) layoutsubviews;

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

MainView-(void) DrawRect: (cgrect) rect;

15. Because the default all the amount of UI control is painted, in this step is to put all the things up, sometimes need to use the knowledge of the quartz2d is in this method, but also to be careful not to forget to write super, otherwise the system originally things will not be drawn up, It is recommended to use a Bezier path as much as possible, since the system's default context representation can sometimes leak memory. The DrawRect method can only be called once at load time, if you need to call it later, such as the arc of the download progress, you need to always brush the frame, you will use Setneedsdisplay to call this method several times

Childview-(void) DrawRect: (cgrect) rect;

16.view child controls inside the drawing method, sometimes you can customize the label in the middle with a strikethrough (used to write the original price before the discount) is here to draw a root line.

-(void) Viewdidappear: (BOOL) animated;

17. Finish drawing all the above, it will show that the view is fully loaded. The operation here may be to set some animation of the page, or to set the TABLEVIEW,COLLECTIONVIEW,QQ chat page to scroll to the bottom of the Scrolltoindexpath and other code operations.

-(void) Applicationdidbecomeactive: (uiapplication *) application;

18. Finally this is the Appdelegate application to get the focus method, really here, is all things loaded, the application is ready to stay the best state waiting for user action. This method in a class to write about pop-up keyboard method, such as some user login interface in order to better user experience, let you just open the program to login interface, the focus of the cursor will automatically flash in the text box of the account, that is, set the account text box as the first responder. The keyboard pops up from below when the page is loaded, and this code is typically written in this method.

If you do not see this article in the Dong Platinum Blog Park, please click to view the original

The above summary, there are different views of the welcome exchange, welcome attention

Sequence analysis of calls to all methods at application startup

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: info-contact@alibabacloud.com 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.