When a view controller is created and displayed on the screen. The order in which the code runs
1, Alloc create objects, allocate space
2. Init (Initwithnibname) initializes the object, initializes the data
3, Loadview from 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 is complete. Ability to define data yourself and dynamically create other controls
5, Viewwillappear View will appear today before the screen, immediately this view will be shown on the screen now
6. Viewdidappear view has been rendered on the screen
The order in which a view is removed from the screen and destroyed. This order is almost identical to the opposite of the above
1. Viewwilldisappear view will be removed from the screen before running
2, Viewdiddisappear view has been removed from the screen, the user can not see this view
3. The Dealloc view is destroyed. The objects you created in Init and viewdidload need to be released here
About Viewdidunload: Assuming that this view is not the view being displayed on the current screen when a memory warning occurs, Viewdidunload will be run and all child views of this view will be destroyed. To free up memory, developers need to manually free memory for objects created in Viewload, Viewdidload. As 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 when the view is called. The order in which these methods are called needs to be sorted out.
Usually the above method contains for example the following several. These methods are all 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 when it executes.
1)-(void) viewdidload.
An app loads the view into memory first by calling the Loadview method or by loading the initial interface created in IB. The Viewdidload method is then called to make further settings. Usually. We load the various initial data. Very much content such as initial settings. will be implemented in this way. So This method is a very important method that is not used frequently.
Note, however, that this method will only be called once when the app is loaded and will not be called again, so it can only be used for initial setup.
2)-(void) viewdidunload;
In case of sufficient memory. The view of the software is generally kept in memory, but assumes that there is not enough memory. Some viewcontroller that are not being displayed receive a warning of insufficient memory, and then release the view that you own to achieve the purpose of freeing up memory. However, the system simply frees up memory and does not release the full rights of the object, so it is often necessary to place the entire right of the object that is not required to be kept in memory, that is, to set its 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 because we need to make sure that we can react to it when we receive a memory warning. So this method usually needs to be realized.
Also, even after the home key has been pressed on the device. The system does not necessarily call this method, because after IOS4, the system agrees to suspend the app in the background. And keep it stuck in memory. Therefore, Viewcontroller does not call this method to clear memory.
3)-(void) Viewwillappear: (BOOL) animated;
After the system has loaded 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. Like what. We can use this method to set how the device should be displayed in different directions.
On the other hand, when the app has multiple views. When you switch between views. The Viewdidload method is not loaded again, so assume when the view is being transferred. The need to update the data can only be achieved within this method.
So this method is also very often used.
4)-(void) Viewdidappear: (BOOL) animated;
Sometimes, for some special reason, we can't be in the Viewwillapper method. Update the view.
You can override this method to further set the view that is being displayed here.
5)-(void) Viewwilldisappear: (BOOL) animated.
When the view is transformed, the current view is about to be removed or overwritten. This method is called to do some aftercare and setup.
Because after the IOS4. The system agrees to suspend the app in the background, so after you press the home key. This method is not called by the system. As far as the app itself is concerned. The view displayed by the app is still the view at the time of the hang, so this method is not called.
6)-(void) Viewdiddisappear: (BOOL) animated.
We can rewrite this method. Do some other work on views that have disappeared, or that have been overwritten, or that have been hidden.
The difference between IOS development Loadview and Viewdidload
These two methods are indispensable for iphone development. They can all be used when the view is loaded. Initialize some of the content. But what difference do they have?
Viewdidload This method is only called 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: [UIScreen
- Mainscreen] . Applicationframe] ;
- [View Setbackgroundcolor:_color] ;
- self.view = view;
- [View release] ;
- }
You implement the Loadview method in the controller. Then you may be called by the memory management control at some point in the application execution.
Assuming that the device is out of memory, the view controller receives a didreceivememorywarning message.
The default implementation is to check whether the current controller's view is in use.
Suppose its view is not inside the view hierarchy that is currently in use, and your controller implements the Loadview method. Then this view will be release, and the Loadview method will be re-used to create a new view.
Uiviewcontroller life cycle and iOS program run sequence