"Sharing" Uiviewcontroller life cycle Simple Induction

Source: Internet
Author: User

First, call Order:

1. + (ID) alloc allocate memory;

2.-(ID) Init method (including other-(ID) init ... method), which is allowed to be called only once, and to be written with the Alloc method, the memory requested in the Init method is freed (or elsewhere) in the Dealloc method;

3.-(void) Awakefromnib This method is called after initialization with Xib, and this method is not generally overridden;

4.-(void) Loadview do not override this method if you are using Xib to create the Viewcontroller. This method is not generally modified;

5.-(void) Viewdidload view is called after the load is complete, this method is important to add some of your own defined controls, note that the frame of the view is not necessarily the frame at the time of display, the real frame will be in-(void) Viewdidappear: After. In the ios6.0+ version, it is only called once throughout the lifetime of the object, and it is important to note that the ios3.0~ ios5.x version may be repeated calls, when Viewcontroller received a memory warning, will release the view, and call Viewdidunload, and then re-call Viewdidload, so to support iOS6.0 Previous versions of children's shoes to pay attention to the memory management here. 6.-(void) Viewwillappear: (BOOL) Animated view will be displayed when you can load some pictures here, and some other memory-occupying resources;
7.-(void) Viewdidappear: (BOOL) Animated view has been shown at the time;

8.-(void) Viewwilldisappear: (BOOL) When animated view is going to be hidden, you can release some of the larger memory-intensive resources before reloading in viewwillappear:. such as: Picture/sound/video. If the view is hidden and the resources in memory that are not called before the display are retained, then the chance of app Flash will increase, especially when the Viewcontroller is more;

9.-(void) Viewdidappear: (BOOL) when the animated view has been hidden;

Dealloc-(void), do not call this method manually, the system will call this method automatically when the reference count value is 0.



Second, use Navigationcontroller to Push to switch the displayed view, the order of calls:

For example, from A controller Push display B Controller,

[(A *) Self.navigationcontroller pushviewcontroller:b Animated:yes]

1. Load the view of the B controller (if not);

2. Call a-(void) Viewwilldisappear: (BOOL) animated;

3. Call B-(void) Viewwillappear: (BOOL) animated;

4. Call a-(void) Viewdiddisappear: (BOOL) animated;

5. Call B-(void) Viewdidappear: (BOOL) animated;

In summary, the Viewcontroller switch is to call the hidden method first, then invoke the displayed method, call will before calling do.

Third, re-layout view of the child view

-(void) Viewwilllayoutsubviews

-(void) viewdidlayoutsubviews
The two methods are called when the view is laid out for his child view, including the view display/hide/screen rotation.

If the design of the application needs to support multi-direction can be in here to do some UI adaptation of the vertical and horizontal screen.

Print Appendix:

2014-05-04 11:52:03.461 testlifecycle[3141:60b] [Viewcontroller viewdidload]

2014-05-04 11:52:03.463 testlifecycle[3141:60b] [Viewcontroller viewwillappear:]

2014-05-04 11:52:03.464 testlifecycle[3141:60b] [Viewcontroller viewwilllayoutsubviews]

2014-05-04 11:52:03.465 testlifecycle[3141:60b] [Viewcontroller viewdidlayoutsubviews]

2014-05-04 11:52:03.516 testlifecycle[3141:60b] [Viewcontroller viewdidappear:]

#Push一个新的ViewController: Testviewcontroller

2014-05-04 11:52:07.219 testlifecycle[3141:60b] [Viewcontroller viewwilllayoutsubviews]

2014-05-04 11:52:07.220 testlifecycle[3141:60b] [Viewcontroller viewdidlayoutsubviews]

2014-05-04 11:52:07.298 testlifecycle[3141:60b] [Testviewcontroller viewdidload]

2014-05-04 11:52:07.299 testlifecycle[3141:60b] [Viewcontroller viewwilldisappear:]

2014-05-04 11:52:07.299 testlifecycle[3141:60b] [Testviewcontroller viewwillappear:]

2014-05-04 11:52:07.303 testlifecycle[3141:60b] [Viewcontroller viewwilllayoutsubviews]

2014-05-04 11:52:07.303 testlifecycle[3141:60b] [Viewcontroller viewdidlayoutsubviews]

2014-05-04 11:52:07.304 testlifecycle[3141:60b] [Testviewcontroller viewwilllayoutsubviews]

2014-05-04 11:52:07.304 testlifecycle[3141:60b] [Testviewcontroller viewdidlayoutsubviews]

2014-05-04 11:52:07.806 testlifecycle[3141:60b] [Viewcontroller viewdiddisappear:]

2014-05-04 11:52:07.807 testlifecycle[3141:60b] [Testviewcontroller viewdidappear:]

2014-05-04 11:52:07.807 testlifecycle[3141:60b] [Testviewcontroller viewwilllayoutsubviews]

2014-05-04 11:52:07.808 testlifecycle[3141:60b] [Testviewcontroller viewdidlayoutsubviews]

return in #在新的ViewController

2014-05-04 11:52:10.606 testlifecycle[3141:60b] [Testviewcontroller viewwilldisappear:]

2014-05-04 11:52:10.606 testlifecycle[3141:60b] [Viewcontroller viewwillappear:]

2014-05-04 11:52:11.108 testlifecycle[3141:60b] [Testviewcontroller viewdiddisappear:]

2014-05-04 11:52:11.109 testlifecycle[3141:60b] [Viewcontroller viewdidappear:]

"Sharing" Uiviewcontroller life cycle Simple Induction

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.