IOS life cycle

Source: Internet
Author: User

Idle time want to summarize the life cycle of this piece, create a new test project, first look atAppDelegate类方法的调用,在所有的方法中加入

NSLog (@ "---%s---", __func__), printing method name

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

Override point for customization after application launch.

NSLog (@ "---%s---", __func__);

return YES;

}

-(void) Applicationwillresignactive: (uiapplication *) Application {

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 pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.

NSLog (@ "---%s---", __func__);

}

-(void) Applicationdidenterbackground: (uiapplication *) Application {

Use the This method to release the shared resources, save user data, invalidate timers, and store enough application state info Rmation 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 the User quits.

NSLog (@ "---%s---", __func__);

}

-(void) Applicationwillenterforeground: (uiapplication *) Application {

Called as part of the transition from the background to the active state; Here's can undo many of the changes made on entering the background.

NSLog (@ "---%s---", __func__);

}

-(void) Applicationdidbecomeactive: (uiapplication *) Application {

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 (@ "---%s---", __func__);

}

-(void) Applicationwillterminate: (uiapplication *) Application {

Called when the application are about to terminate. Save data if appropriate. See also Applicationdidenterbackground:.

NSLog (@ "---%s---", __func__);

}

Check the sequence of method calls by starting the program, entering the background, and waking from the background into the foreground:

The method is called when the program just starts:

[Appdelegate application:didfinishlaunchingwithoptions:]

[Appdelegate applicationdidbecomeactive:]//program is activated

The method is called when the program enters the background:

[Appdelegate applicationwillresignactive:]//Program deactivation Status

[Appdelegate Applicationdidenterbackground:]//program into the background

To invoke the method from the background wake-up into the foreground:

[Appdelegate Applicationwillenterforeground:]//program into the foreground

[Appdelegate applicationdidbecomeactive:] //program is activated

Analysis:

1. application:didFinishLaunchingWithOptions :
The program is executed at startup for the first time, typically creating a Window object in this function that renders the program content to the user through window.

2.applicationWillResignActive (inactive)

The program is going to lose Active state when called, such as have 电话 come in or press Home键 , then the program into the background state, the corresponding applicationWillEnterForeground(即将进入前台) method.

The function mainly performs the following actions:
A. Pausing a task that is being performed
B. No timer
C. reduced OpenGL ES frame rate
D. If the game should be paused

3.applicationDidEnterBackground(已经进入后台)
CorrespondingapplicationDidBecomeActive(已经变成前台)

This method is used to:
A. Releasing a shared resource
B. Save user data (write to hard disk)
C. void Timer
D. Save enough program status for the next repair;

4.applicationWillEnterForeground(即将进入前台)
When the program is about to enter the foreground, call, correspond applicationWillResignActive(即将进入后台) ,
This method is used to: Undo the applicationWillResignActive changes made.

5.applicationDidBecomeActive(已经进入前台)
Called when the program has changed Active(前台) . Corresponding applicationDidEnterBackground(已经进入后台) .
Note: 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 as a applicationDidEnterBackground method.

The life cycle of Viewcontroller:

Create two VC, one created with SB, a pure code creation

-(void) Viewdidload {

[Super Viewdidload];

Self.title = @ "Second page (SB)";

Self.view.backgroundColor = [Uicolor Whitecolor];

NSLog (@ "%s", __func__);

Additional setup after loading the view, typically from a nib.

}

-(Instancetype) init{

self = [super init];

NSLog (@ "%s", __func__);

return self;

}

-(Instancetype) Initwithnibname: (NSString *) Nibnameornil Bundle: (NSBundle *) Nibbundleornil {

NSLog (@ "%s", __func__);

if (self = [super Initwithnibname:nibnameornil Bundle:nibbundleornil]) {

}

return self;

}

If the concatenation diagram is connected storyboard go this way

-(Instancetype) Initwithcoder: (Nscoder *) Adecoder {

NSLog (@ "%s", __func__);

if (self = [Super Initwithcoder:adecoder]) {

}

return self;

}

Xib Loading complete

-(void) awakefromnib {

[Super awakefromnib];

NSLog (@ "%s", __func__);

}

Load view (default from NIB)

-(void) Loadview {

NSLog (@ "%s", __func__);

Self.view = [[UIView alloc] Initwithframe:[uiscreen mainscreen].bounds];

Self.view.backgroundColor = [Uicolor Redcolor];

}

The view will appear

-(void) Viewwillappear: (BOOL) Animated {

NSLog (@ "%s", __func__);

[Super viewwillappear:animated];

}

View is about to layout its subviews

-(void) Viewwilllayoutsubviews {

NSLog (@ "%s", __func__);

[Super Viewwilllayoutsubviews];

}

View has been layout its subviews

-(void) Viewdidlayoutsubviews {

NSLog (@ "%s", __func__);

[Super Viewdidlayoutsubviews];

}

View already appears

-(void) Viewdidappear: (BOOL) Animated {

NSLog (@ "%s", __func__);

[Super viewdidappear:animated];

}

The view is going to disappear

-(void) Viewwilldisappear: (BOOL) Animated {

NSLog (@ "%s", __func__);

[Super viewwilldisappear:animated];

}

The view has disappeared

-(void) Viewdiddisappear: (BOOL) Animated {

NSLog (@ "%s", __func__);

[Super viewdiddisappear:animated];

}

-(void) didreceivememorywarning {

[Super didreceivememorywarning];

NSLog (@ "%s", __func__);

Dispose of any resources the can be recreated.

}

The order of execution of the methods can be seen according to the printed results, SB creates:

2018-03-26 13:46:48.268882+0800 test[4659:680728]-[secondviewcontroller Initwithcoder:]

2018-03-26 13:46:48.269376+0800 test[4659:680728]-[secondviewcontroller awakefromnib]

2018-03-26 13:46:48.270668+0800 test[4659:680728]-[secondviewcontroller Loadview]

2018-03-26 13:46:48.271432+0800 test[4659:680728]-[secondviewcontroller Viewdidload]

2018-03-26 13:46:48.271676+0800 test[4659:680728]-[secondviewcontroller viewwillappear:]

2018-03-26 13:46:48.281261+0800 test[4659:680728]-[secondviewcontroller viewwilllayoutsubviews]

2018-03-26 13:46:48.281423+0800 test[4659:680728]-[secondviewcontroller viewdidlayoutsubviews]

2018-03-26 13:46:48.783592+0800 test[4659:680728]-[secondviewcontroller viewdidappear:]

The print order for pure Code creation is:

2018-03-26 13:47:19.564963+0800 test[4659:680728]-[thirdviewcontroller Initwithnibname:bundle:]

2018-03-26 13:47:19.565357+0800 test[4659:680728]-[thirdviewcontroller init]

2018-03-26 13:47:19.566861+0800 test[4659:680728]-[thirdviewcontroller Loadview]

2018-03-26 13:47:19.567676+0800 test[4659:680728]-[thirdviewcontroller Viewdidload]

2018-03-26 13:47:19.568003+0800 test[4659:680728]-[thirdviewcontroller viewwillappear:]

2018-03-26 13:47:19.580727+0800 test[4659:680728]-[thirdviewcontroller viewwilllayoutsubviews]

2018-03-26 13:47:19.580972+0800 test[4659:680728]-[thirdviewcontroller viewdidlayoutsubviews]

2018-03-26 13:47:20.084203+0800 test[4659:680728]-[thirdviewcontroller viewdidappear:]



* * Analysis * *
1. initWithNibName:bundle :
Initializes UIViewController , performs critical data initialization operations, and StoryBoard calls this method for non-creation UIViewController .
* * Note: Do not do this here View , View initialize in the loadView method. **

2.initWithCoder:
If StoryBoard view management is used, the program is not initialized directly UIViewController , StoryBoard automatically initialized segue , or automatically initialized when triggered, so the method is initWithNibName:bundle not called, but is initWithCoder called.

3.awakeFromNib
When a awakeFromNib method is called, all views are outlet action connected, but not yet determined, and this method can be used as an instantiation mate for the view controller, because some content that needs to be set according to user preferences cannot exist storyBoard or xib , so it can be awakeFromNib loaded in the method.

4.loadView
When executing to a loadView method, if the view controller is nib created by, then the view controller has been unpacked from the nib file and created, and the next task is to view initialize it.

loadViewThe method is UIViewController called when the object is view accessed and is empty. This is awakeFromNib a difference between it and the method.
Suppose we dispose of a property when dealing with a memory warning view : self.view = nil . Therefore loadView , the method may be called multiple times during the life cycle of the view controller.
loadViewMethods should not be called directly, but are called by the system. It loads or creates a view and assigns it a value to UIViewController the view property.

In the view process of creation, the first is nibName to find the corresponding nib file and then load. If it is nibName empty or cannot find the corresponding nib file, an empty view is created (this is typically pure code)

Note: Do not invoke the method of the parent class when overriding the Loadview method.

5.viewDidLoad
When loadView view it is loaded into memory, the method is further called viewDidLoad to make further settings. At this point, the view level has been put into memory, usually, we have a variety of initialization data loading, initial settings, modify constraints, remove views and many other operations can be implemented in this method.

View hierachy: Because each view has its own child view, this view hierarchy can also be understood as a tree-like data structure. and the root node of the tree, that is 根视图(root view) , in the UIViewController view attribute. It can be seen as a container for all other child views, that is, the root node.
6.viewWillAppear
After loading all the data, the system will display the view on the screen, this method will be called first, and usually we will make further settings in this method for the view that will be displayed. For example, how to display the device in different directions, set the status bar orientation, set the view display style, and so on.

On the other hand, when APP there are multiple views, the hierarchy view switch will also call this method, if the view is called when the data needs to be updated, it can only be implemented within this method.

7.viewWillLayoutSubviews
viewis about to lay it out Subviews . Such view as bounds changes (for example: the status bar is never displayed to the display, the view direction changes), to adjust Subviews the position, before the adjustment to do the work can be put in the method to implement

8.viewDidLayoutSubviews
viewIt has been laid out Subviews , where you can place the work that needs to be done after the adjustment is complete.

9.viewDidAppear
This method is called when view is added to the view level and multi-view, and the view that is being displayed can be further set here.

10.viewWillDisappear
When the view is toggled, the current view is about to be removed, or is overwritten, and the method is called, which is not called at this time removeFromSuperview .

11.viewDidDisappear
viewHas disappeared or been overwritten, has been called at this time removeFromSuperView ;

12.dealloc
The view is destroyed, and this time you need to init release the objects you created in and viewDidLoad .

13.didReceiveMemoryWarning
In the case of sufficient memory, app the view is usually kept in memory, but if there is not enough memory, there will be an out-of-memory warning that is not being displayed, viewController and then it will release the view that you own to achieve the purpose of freeing the memory. But the system only frees up memory and does not release the ownership of the object, so usually we need to take ownership of the object that does not need to be displayed in memory and place its pointer on it nil .



The life course of the view
  • [ViewController initWithCoder:] or [ViewController initWithNibName:Bundle] : first load the object from the archive file UIViewController . Even pure code is nil passed as a parameter to the latter.
  • [UIView awakeFromNib]:As an assistant to the first method, the method handles some additional settings.
  • [ViewController loadView] : creates or loads a view property that assigns it to a UIViewController value view .
    - [ViewController viewDidLoad] : The whole 视图层次(view hierarchy) is now in memory, you can remove some views, modify constraints, load data, and so on.
  • [ViewController viewWillAppear:] : The view load is complete and will be displayed on the screen. Not yet animated, you can change the current screen orientation or status bar style, and so on.
  • [ViewController viewWillLayoutSubviews]The child view location layout is about to begin
  • [ViewController viewDidLayoutSubviews]The location layout for the notification view has been completed
  • [ViewController viewDidAppear:] : The view has been displayed on the screen, so you can make some changes to the display effect on the image.
  • [ViewController viewWillDisappear:] : The view is about to disappear
  • [ViewController viewDidDisappear:] : The view has disappeared
  • [ViewController dealloc:] : Called when the view is destroyed


Reference Link: https://www.jianshu.com/p/d60b388b19f5

IOS life cycle

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.