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];}