Boot execution sequence for iOS programs appdelegate and Uiviewcontroller life cycle
The status switch for iOS apps is important, and uiviewcontroler is especially important for the MVC model of iOS, which is basically inherited from him.
First, the boot execution order of iOS programs
1 Entrance to the program
Enter the main function and set the agent appdelegate called function
2 program completed loading
-[appdelegate Application:didfinishlaunchingwithoptions:]
3 Creating Window Windows
4 program is activated
-[appdelegate Applicationdidbecomeactive:]
5 When you click Command+h
Program Deactivation Status
-[appdelegate Applicationwillresignactive:]
program into the background
-[appdelegate Applicationdidenterbackground:]
6 clicks into the project
Procedures to enter the foreground
-[appdelegate Applicationwillenterforeground:]
Program is activated
-[appdelegate Applicationdidbecomeactive:]
1, for applicationwillresignactive (inactive) and Applicationdidenterbackground (backstage) The difference between the two .
(1) applicationwillresignactive (inactive):
For example, when a phone comes in or a text message comes in, or a lock screen , and so on, your app hangs into an inactive state, that is, your phone actually displays your current app window , but Be forcibly occupied by another task , or in a background state (because you want to go into an inactive state and then enter the background).
(2)applicationdidenterbackground (Backstage):
refers to the current window is not your app, most programs enter this background will stay in this state for a while , the time will go into the suspended state (Suspended). If your program is specially processed , it can be in the background state or in the background state. Suspended(hangs): The program cannot execute code in the background. The system automatically turns the program into this state and does not give notice. When suspended, the program is still in memory, when the system memory is low, the system will remove the suspended program, to provide more memory for the foreground program.
Look at the following flowchart:
This diagram is particularly important and shows all the switching states.
Entry function:
int Main (intChar * argv[]) { @autoreleasepool {
return class]));
} }
2.UIApplicationMain function Explanation :
Official explanation
IF Nil is specified for Principalclassname, the value of Nsprincipalclass from the info.plist is used. If there is no//Nsprincipalclass key specified, the UIApplication class is used. The delegate class would be instantiated using init. Uikit_extern int uiapplicationmain (int argc, char *argv[], nsstring *principalclassname, NSString *delegateclassname);
After translation (explanation of parameters)
The 1.argc and argv parameters are intended to be consistent with the C language , which is useless in this, not detailed.
2. The following two parameters are principalclassname (main class name) and Delegateclassname (delegate class name).
(1) If Principalclassname is nil, then its value will be obtained from info.plist, and if not in Info.plist, the default is UIApplication. Principalclass This class does nothing except manage the entire program's life cycle, it only listens for events and then gives it to Delegateclass.
(2) Delegateclass will instantiate an object when the project is new. Nsstringfromclass ([Appdelegate class])//equivalent @ "Appdelegate"
3.AppDelegate Class implementation file
-(BOOL) Application: (UIApplication *) application didfinishlaunchingwithoptions: (nsdictionary *) launchOption{//Override point for customization after application launch.NSLog (@"didfinishlaunchingwithoptions"); returnYES;}- (void) Applicationwillresignactive: (uiapplication *) Application {/*when an application changes from active state (active) to inactive (inactive is triggered by a call, this can happen under some temporary interruption (for example: To call, to SMS) or when the program exits, he transitions to the background and then terminate Use this method to pause the task in progress, disable the timer, throttle the OpenGL ES frame rate. In the game you should pause the game in this method. */ //Sent when the application are about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as a incoming phone call or SMS message) or when the US Er quits the application and it begins the transition to the background state. //Use the This method to the pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.NSLog (@"willresignactive");}- (void) Applicationdidenterbackground: (uiapplication *) Application {/*Use this method to free up shared resources, save user data, invalid timers, and store enough application state information to restore the current state of your application in case it terminates lost data. If your program supports running in the background, applicationwillterminate is not called when the user exits. */ //Use the method to release the shared resources, save user data, invalidate timers, and store enough application state in Formation to the restore your application to the it is terminated later. //If Your application supports background execution, this method is called instead of Applicationwillterminate:when th E user quits.NSLog (@"Didenterbackground"); }- (void) Applicationwillenterforeground: (uiapplication *) Application {/*switch from background to inactive and then into active state. */ //Called as part of the transition from the background to the inactive state; Here you can undo many of the changes mad E on entering the background.NSLog (@"Willenterforeground");}- (void) Applicationdidbecomeactive: (uiapplication *) Application {/*Restarts All tasks, whether from inactive or just starting a program, or background state. */ //Restart Any tasks this were paused (or not yet started) while the application is inactive. If the application is previously in the background, optionally refresh the user interface.NSLog (@"didbecomeactive"); }- (void) Applicationwillterminate: (uiapplication *) Application {/*Terminate, game over*/ //Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.NSLog (@"willterminate");}
The following shows the sequence of interactions between them:
Start the program
2014-07-28 15:22:39.883 lifecycle[3024:a0b] Didfinishlaunchingwithoptions
2014-07-28 15:22:39.887 lifecycle[3024:a0b] Didbecomeactive
Press the Home button
2014-07-28 15:22:43.130 lifecycle[3024:a0b] Willresignactive
2014-07-28 15:22:43.131 lifecycle[3024:a0b] Didenterbackground
Re-click the program
2014-07-28 15:22:44.380 lifecycle[3024:a0b] Willenterforeground
2014-07-28 15:22:44.380 lifecycle[3024:a0b] Didbecomeactive
Analysis:
1.application:didfinishlaunchingwithoptions:
The program has been executed for the first time, and if it is started directly, there is no data in the launchoptions; otherwise, launchoptions will contain the content of the corresponding method (such as starting Tempo master from it).
2.applicationWillResignActive (inactive)
Called when the program is about to lose its active state, such as pressing the home key or having a phone message coming in. The program will then enter the background state. The corresponding Applicationwillenterforeground method is used to
A. Suspension of the tasks under implementation;
b, the timer is forbidden;
C, reduce OpenGL es frame rate;
D, if the game should be suspended game;
3.applicationDidEnterBackground (has entered the background)
The program has been called in the background, corresponding to the applicationdidbecomeactive (has become the foreground), this method is used to
A, release shared resources;
b, save user data (write to hard disk);
c, obsolete timer;
D. Save sufficient program status for the next recovery;
4.applicationWillEnterForeground (will enter the front desk)
The program is about to go into the foreground when called, corresponding to Applicationwillresignactive (will enter the background). This method is used to
1. Undo the changes made in applicationwillresignactive.
5.applicationDidBecomeActive (has entered the front desk)
Called when the program has become active (foreground). Corresponds to the Applicationdidenterbackground (has entered the background).
1. If the program is in the background, refresh the user interface in this method.
6.applicationWillTerminate
Called when the program is about to exit. Remember to save the data, like the Applicationdidenterbackground method.
In fact, looking closely at the above state diagram and output printing, he explained all of these switching interaction processes.
Second, the life cycle of Uiviewcontroller
//The designated initializer//The specified initialization method for this uiviewcontroller (the other initialization method eventually calls the initialization method);//If the concatenation diagram is connected storyboard No need to take care of the goods.-(ID) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil {NSLog (@"%s", __function__); Self=[Super Initwithnibname:nibnameornil Bundle:nibbundleornil]; if(self) {//Custom Initialization } returnSelf ; } //View controller In view loading complete, Viewcontroller self-contained view load complete- (void) viewdidload {NSLog (@"%s", __function__); [Super Viewdidload]; //Do any additional setup after loading the view.}//Memory Warning appears//Analog Memory Warning: Click Simulator->hardware-> simulate Memories Warning- (void) didreceivememorywarning {NSLog (@"%s", __function__); [Super didreceivememorywarning]; //Dispose of any resources the can be recreated.}//The view will appear- (void) Viewwillappear: (BOOL) animated {NSLog (@"%s", __function__); [Super viewwillappear:animated];}//View already appears- (void) Viewdidappear: (BOOL) animated {NSLog (@"%s", __function__); [Super viewdidappear:animated];}//The view is going to disappear//Double-click the home key to roll up the program to execute the function- (void) Viewwilldisappear: (BOOL) animated {NSLog (@"%s", __function__); [Super viewwilldisappear:animated];}//The view has disappeared- (void) Viewdiddisappear: (BOOL) animated {NSLog (@"%s", __function__); [Super viewdiddisappear:animated];} @end
Look at the print output:
2015-01-03 17:39:03.973 lessonuiviewcontroler[791:41458]-[rootviewcontroller Initwithnibname:bundle:]
2015-01-03 17:39:03.976 lessonuiviewcontroler[791:41458]-[rootviewcontroller Viewdidload]
2015-01-03 17:39:03.976 lessonuiviewcontroler[791:41458]-[rootviewcontroller viewwillappear:]
2015-01-03 17:39:04.071 lessonuiviewcontroler[791:41458]-[rootviewcontroller viewdidappear:]
2015-01-03 17:39:11.630 lessonuiviewcontroler[791:41458] Received memory warning.
2015-01-03 17:39:11.631 lessonuiviewcontroler[791:41458]-[rootviewcontroller didreceivememorywarning]
2015-01-03 17:39:17.581 lessonuiviewcontroler[791:41458]-[rootviewcontroller viewwilldisappear:]
2015-01-03 17:39:17.581 lessonuiviewcontroler[791:41458]-[rootviewcontroller viewdiddisappear:]
When a view controller is created and displayed on the screen. Order of execution of code
1, Alloc create objects, allocate space;
2, init (initwithnibname) Initialize the object, initialize the data;
3, Loadview from the nib load view, usually this step does not need to interfere. Unless you are not using the Xib file to create the view;
4, Viewdidload loading completed, can be customized data and dynamic creation of other controls;
5, Viewwillappear View will appear in front of the screen, immediately this view will be displayed on the screen;
6. Viewdidappear view has been rendered on the screen the order of execution when a view is removed from the screen and destroyed is almost the opposite of the above;
1, Viewwilldisappear
The view will be removed from the screen before the execution
2, Viewdiddisappear
The view has been removed from the screen and the user cannot see the view.
3, Dealloc
The view is destroyed, and you need to release the objects you created in Init and viewdidload about Viewdidunload: If this view is not on the current screen when a memory warning occurs
The view that is being displayed,
Viewdidunload will be executed and all child views of this view will be destroyed to free up memory, at which point developers need to manually free memory for objects created in Viewload, Viewdidload.
Because when the view is displayed again on the screen, Viewload and viewdidload are called again to construct the view again. When we create a pair of Uiviewcontroller class
Typically, the system generates several default methods, most of which are related to the invocation of the view, but the order in which these methods are called when the view is called, needs to be sorted out. Usually on
The methods include the following, which are methods of the Uiviewcontroller class:
-(void) viewdidload;
-(void) viewdidunload;
-(void) Viewwillappear: (BOOL) animated;
-(void) Viewdidappear: (BOOL) animated;
-(void) Viewwilldisappear: (BOOL) animated;
-(void) Viewdiddisappear: (BOOL) animated;
The following describes the order in which the app is called at runtime.
1)-(void) viewdidload; an app loads the view into memory first by invoking the Loadview method or by loading the initial interface created in IB. Then you will
Call the Viewdidload method to make further settings. In general, we implement many of the initial data loading, initial setting, and so on, so this method is
A very common, very important method.
Note, however, that this method will only be called once when the app starts to load and will not be called again, so it can only be used for initial setup.
2)-(void) viewdidunload; In the case of sufficient memory, the view of the software is usually kept in memory, but if there is not enough memory, some viewcontroller that are not being displayed
will receive a warning of insufficient memory and then release the view that you own to achieve the purpose of freeing up memory. But the system only frees up memory and does not release ownership of the object, so usually we
You need to take ownership of objects that do not need to be persisted in memory here, that is, set their pointer to nil. This method is not usually called when the view is transformed, but only in the system
Will not be invoked until the system exits or a memory warning is received. But since we need to make sure we can react to it when we receive a memory warning, this approach usually needs to be implemented.
In addition, the system does not necessarily call this method even after the home key is pressed on the device, because after IOS4, the system allows the app to hang in the background and remain in memory, so
Viewcontroller does not call this method to clear memory.
3)-(void) Viewwillappear: (BOOL) animated; After loading all the data, the view will be displayed on the screen, and this method will be called first. Usually we use this method,
Make further settings for the view that will be displayed. For example, we can use this method to set how the device will be displayed in different directions. On the other hand, when the app has multiple views, the
When switching between views, the Viewdidload method is not loaded again, so if the data needs to be updated when the view is entered, it can only be implemented within this method. So this method is also very common.
4)-(void) Viewdidappear: (BOOL) animated;
Sometimes, for some special reason, we cannot update the view in the Viewwillapper method. Then you can override this method to further set the view that is being displayed.
5)-(void) Viewwilldisappear: (BOOL) animated;
When the view is transformed, the current view is called when it is about to be removed or overwritten, and this method is invoked to handle and set up some aftercare.
Since after IOS4, the system allows the app to hang in the background, the system does not call this method after the home button is pressed, because the app itself, the view that is displayed, is still pending
View, so this method is not called.
6)-(void) Viewdiddisappear: (BOOL) animated;
We can override this method and do some other work on the view that has disappeared, or is overwritten, or has been hidden. The flowchart of the above method can be expressed simply as follows:
Run app-> load view, call the Viewdidload method, call the Viewwillappear method, call the Viewdidappear method, and run normally
iOS program execution sequence appdelegate and Uiviewcontroller life cycle