Ios-viewcontroller life cycle

Source: Internet
Author: User

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) initializes the object, initializes the data
3, Loadview from the nib load view, usually this step does not need to interfere. Unless you're not using the Xib file to create a view
4, Viewdidload loading complete, can be customized data and dynamic creation of other controls
5, Viewwillappear View will appear before the screen, the view will be displayed on the screen immediately
6, Viewdidappear view has been rendered on the screen completed

When a view is removed from the screen and the execution order is destroyed, the order is almost the opposite of the above
1. Viewwilldisappear view will be removed from the screen before execution
2, Viewdiddisappear view has been removed from the screen, the user can not see this view
3. Dealloc view is destroyed, you need to release the objects you created in Init and viewdidload

About Viewdidunload: In the event of a memory warning, if this view is not the view being displayed on the current screen, Viewdidunload will be executed and all child views of this view will be destroyed to free up memory, when developers need to manually viewload, The objects created in the Viewdidload free memory. Because when the view is displayed again on the screen, Viewload and viewdidload are called again to construct the view again.

When we create an object of the Uiviewcontroller class, it is common for the system to generate 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 the above 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. The Viewdidload method is then called to make further settings. In general, we have a lot of content of initial data loading, initial setting and so on, this method is implemented in this way, 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 they own to achieve the purpose of freeing the memory. But the system only frees up memory and does not release ownership of the object, so usually we need to take ownership of objects that do not need to be kept in memory, that is, to set their pointer to nil.

This method is not usually called when the view is transformed, but only when 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, even after the home key is pressed on the device, the system does not necessarily call this method, because after IOS4, the system allows the app to hang in the background and continue to be stuck in memory, so Viewcontroller does not call this method to clear the memory.

3)-(void) Viewwillappear: (BOOL) animated;

After the system loads all the data, the view is displayed on the screen, and this method is called first. We usually use this method to 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, switching between views does not load the Viewdidload method again, so if the data needs to be updated when the view is transferred, 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 key is pressed, because the app itself, the view that the application displays, is still the view at the time of the hang, 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

aaaaaaaa                                                                                              a                                                                                 | 

aaaaaaaa                                                                                               |                                                                                 | 

aaaaaaaa                                                                                               |  Loading New view                                                             | 

aaaaaaaa                                                                                               |                                                                                 | 

aaaaaaaa                                                                                               |                                                                                 v

Release object ownership <-call Viewdidunload <-receive memory warning <-call Viewdiddisappear <-call Viewwilldisappear <-app need to call another view

iOS Program start execution order

http://www.yifeiyang.net/iphone-developer-advanced-3-iphone-application-startup-process/

When the screen edge gesture runs in the push and pop interface, the willappear of one page and the willdisappear of another page are executed simultaneously, without loading viewdidload and Viewload methods

When you push a new view controller

Loadview Viewdidload Viewwilldisapper

The above three methods are inserted in the middle of the viewwilldisapear and Viewdiddisapear methods of the second view controller:

The following programs are available on all two pages:

-(void) Loadview {[Super Loadview];    NSLog (@ "%@---%s", [Self class], __function__);    }-(void) viewdidload {[Super viewdidload];    Additional setup after loading the view, typically from a nib. Self.navigationItem.leftBarButtonItem = [[Uibarbuttonitem alloc] Initwithbarbuttonsystemitem:     Uibarbuttonsystemitemsave target:self Action: @selector (ButtonClick:)]; NSLog (@ "%@---%s", [Self class], __function__);}    When the view is about to appear, it is called-(void) Viewwillappear: (BOOL) animated {[Super viewwillappear:animated]; NSLog (@ "%@---%s", [Self class], __function__);}    When the view is already present, it is called-(void) Viewdidappear: (BOOL) animated {[Super viewdidappear:animated]; NSLog (@ "%@---%s", [Self class], __function__);}    The view is going to disappear, and as soon as a little disappears, it will be called-(void) Viewwilldisappear: (BOOL) animated {[Super viewwillappear:animated]; NSLog (@ "%@---%s", [Self class], __function__);}    The view has completely disappeared and will be called-(void) Viewdiddisappear: (BOOL) animated {[Super viewdidappear:animated]; NSLog (@ "%@---%s", [selF class], __function__);} #pragma mark-buttonclick-(void) ButtonClick: (Uibarbuttonitem *) button {Firstviewcontroller *first = [[Firstviewcont    Roller alloc] init]; [Self.navigationcontroller Pushviewcontroller:first animated:yes];}

In the above 6 methods, the first and the second method are simultaneous, the third method and the fourth method are simultaneous, the fifth and sixth methods are simultaneously.

The difference between IOS development Loadview and Viewdidload

These two methods are essential to the development of the iphone. They can all be used to initialize some content when the view is loaded. But what difference do they have?

Viewdidload This method is called only when view is initialized from the nib file.

Loadview This method is called when the controller's view is nil. This method is used when creating a view programmatically. Such as:

-(void) Loadview {    UIView *view = [[UIView alloc] initwithframe:[Uiscreenmainscreen]. Applicationframe];    [View Setbackgroundcolor:_color];    Self.view = view;    

You implement the Loadview method in the controller, then you may be called by the memory management control at some point when the application is running. If the device is low on memory, the view controller receives a didreceivememorywarning message. The default implementation is to check whether the current controller's view is in use. If its view is not inside the view hierarchy currently in use and your controller implements the Loadview method, the view will be release and the Loadview method will be re-used to create a new view.

--------------------------------------------------------------------------------------------------------------- -----------------------------

Don ' t read Self.view In-loadview. Only set it, don ' t get it.

The Self.view property accessor calls -loadview if the view isn ' t currently loaded. There ' s your infinite recursion.

The usual-to-build the view programmatically in-loadview, as demonstrated in Apple ' s pre-interface-builder examples, Is more like this:

UIView   * view  =   [[ UIView alloc ] init ...];  ...  [ view addSubview : whatever ];  [ view addSubview : whatever2 ];  ...  self . view  = view ;  [ view release ];  

and I don ' t blame you to not using IB. I ' ve stuck with this method for all of Instapaper and find myself much more comfortable with it than dealing with IB ' s com Plexities, interface quirks, and unexpected behind-the-scenes behavior.

First, ARC environment
  • life cycle of a single viewcontroller
    • initwithcoder: (Nscoder *) Adecoder: (If using storyboard or xib)
    • Loadview: Load View
    • viewdidload:view loading complete
    • viewwillappear: Controller's view will be displayed
    • Viewwilllayoutsubviews: The controller's view will be layout child controls
    • Viewdidlayoutsubviews: Controller's view layout child controls complete
      During this time, the system may call Viewwilllayoutsubviews, viewdidlayoutsubviews two methods
    • viewdidappear: View full display of the controller
    • Viewwilldisappear: When the controller's view is about to disappear
      The system also calls Viewwilllayoutsubviews, Viewdidlayoutsubviews two methods
    • viewdiddisappear: When the controller's view disappears completely
  • Multiple viewcontrollers Jumps
    • When we click on Push, we first load the next interface before we call the vanishing method of the interface.
    • Initwithcoder: (Nscoder *) Adecoder: ViewController2 (if created with Xib)
    • Loadview:ViewController2
    • Viewdidload:ViewController2
    • Viewwilldisappear:viewcontroller1 will disappear.
    • Viewwillappear: ViewController2 will appear
    • ViewwilllayoutsubviewsViewController2
    • ViewdidlayoutsubviewsViewController2
    • Viewwilllayoutsubviews:viewcontroller1
    • Viewdidlayoutsubviews:viewcontroller1
    • Viewdiddisappear:viewcontroller1 completely disappeared.
    • Viewdidappear: ViewController2 Full appearance
  • Summary:
    -Entire controller declaration period: viewdidlayoutsubviews, Viewwilllayoutsubviews, Viewwillappear, Viewdidload, Viewdidappear, Viewwilldisappear, viewdiddisappear
Second, non-ARC environment
    • Didreceivememorywarning:
      • When the app receives a memory warning, it sends a message to the view controller.
      • The app never calls this method directly, but takes a call when the system determines that there is not enough memory available.
      • 如果你想覆写这个方法来释放一些控制器使用的额外内存,你应该在你的实现方法中调用父类的实现方法。
    • Viewwillunload:(iOS6废除)
      • Called when a view of the controller is removed
      • The view will no longer be purged under low memory conditions so this method will not be called again.
      • Before IOS5, when the low memory situation occurs, the current controller's view Tumen is no longer required, the system will selectively remove these views from these memory. This method is called to perform some empty tasks before the view is actually destroyed. Like what你想要使用这个方法去清空视图的观察者或通知或者记录视图的状态以便当重新读取的时候恢复。
      • After iOS6, you no longer need to clear a reference to the view. Therefore, some other methods of cleanup, such as emptying the observer, are not necessary.
    • Viewdidunload:(iOS6废除)
      • Called when the view is removed from memory
      • The view is no longer destroyed in the memory of the ground so this method is no longer called
      • Prior to IOS5, the current attempt to view the controller's views was no longer required, and the system would be invoked after the view controller's view was destroyed. This method allows you to perform some last chance of emptying the task if your view controller stores separate references to views or child controls, you should use this method to release these references.你也可以使用这个方法去消除一些你再创建视图时候所创建的但是当视图不再的时候不再需要的关于对象的引用。你不应该使用这个方法去释放用户数据或者一些不能轻易被重新创建的数据。
      • After iOS6, it is not necessary to empty references to views and other objects in the controller.
      • When this method is called, the properties of the view are empty.

Ios-viewcontroller 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.