iOS view life cycle and view controller relationships

Source: Internet
Author: User

The view in iOS is an important part of an application, and the implementation of the feature is closely related, and the view controller controls the view, and its importance is self-evident throughout the application.

  View lifecycle and view controller relationship   based on the 5 states of the view, let's take a systematic look at the life cycle of the view controller. The view controller will callback different methods during the different life cycles of the view. Viewdidload method: The view controller has been instantiated and called when the view is loaded into memory, this time the view does not appear. What is typically done in this method is to initialize the view that is being controlled. The   view is visible before and after calling Viewwillappear: Methods and Viewdidappear: Methods; Viewwilldisappear: Methods and Viewdiddisappear: methods are called before and after views are not visible. 4 methods call the corresponding method of the parent class to implement its function, the location of the method can be adjusted according to the actual situation when encoding, see the following code:  [cpp] -(void) Viewwillappear: (BOOL) animated     {   [super Viewwillappear:yes];        }   -( void) Viewwillappear: (BOOL) Animated { [super viewwillappear:yes];  ... ... }   The Viewdidload method is called only once when the application is running, and the above 4 methods can be repeatedly called several times, they are widely used but also have a strong skill. For example, some applications use a gravity accelerometer, which constantly polls the device to get the gravitational acceleration of the device in the z-axis, x-axis, and y-axis in real time. Continuous polling is bound to consume a lot of power and thus affect battery life, we use these 4 methods to open or close the gravity accelerometer in a timely manner to achieve the purpose of saving electricity. How to use these 4 methods to achieve "timely" is a question worth thinking about. The  ios system calls Didreceivememorywarning: and Viewdidunload: Methods in low memory conditions. IOS6 no longer uses viewdidunload:, and only supports didreceivememorywarning:. Didreceivememorywarning: The main function of the method is to free up memory, including some member variables in the view controller and the release of the view. Examples are as follows:  [cpp] -(void) didreceivememorywarning {   self.button = nil;  self.mystringd = nil;  [ MYSTRINGC release];    [super didreceivememorywarning];  }   -(void) didreceivememorywarning { self.button = Nil;self.mystringd = nil;[ MYSTRINGC release]; [super didreceivememorywarning];}

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.