Loadview and viewdidload are two methods that must be used in iPhone development. They can be used to initialize some content during view loading. But what are their differences?
The viewdidload method is called only when the view is initialized from the NIB file. Viewdidload is used for initialization and loading.
The loadview method is called when the Controller's view is nil. This method is used to create a view programmatically.. Loadview is the method called when the view is first loaded when code is used to generate the view. Use (write) code to implement controls. Functions used to generate controls using code. Viewdidload is called whether to load a view from XIB or generate a view from loadview.
For example:
1.-(void) loadview {
2. uiview * view = [[uiview alloc] initwithframe: [uiscreen
3. mainscreen]. applicationframe];
4. [view setbackgroundcolor: _ color];
5. Self. view = view;
6. [view release];
7 .}
If you implement the loadview method in the controller, you may be called by the memory management control at some time when the application is running. If the device memory is insufficient, the View Controller receives the message didreceivememorywarning. The default implementation is to check whether the current controller's view is in use. If its view is not in the current view hierarchy and your controller implements the loadview method, the view will be release, the loadview method is called again to create a new view.
Viewdidunload Method
- This method is called when the system memory is tight (Note: viewcontroller is not dealloc)
- When the memory is tight, didreceivemorywarning before iPhone OS 3.0 is the only way to release useless memory, but the viewDidUnload method is better in OS 3.0 and later versions.
- In this method, set all IBOutlet (whether property or instance variables) to nil (the system release view has already been release)
- This method releases other view-related objects, other objects created at runtime (but not required by the system), objects created in viewDidLoad, and cached data.
- After the release object, set the object to nil (IBOutlet only needs to set it to nil, and the release view has been released by the System)
- ViewDidUnload is generally considered as the image of viewDidLoad, because when the view is requested again, viewDidLoad will be re-executed
- Objects release in viewDidUnload must be easily re-created (for example, objects created in viewDidLoad or other methods ), do not release user data or other objects that are difficult to recreate
Dealloc Method
- ViewDidUnload is not associated with the dealloc method, so dealloc continues to do what it should do.
For example:
-(Void) viewdidunload {
Self. startbutton = nil;
[Setupviewcontroller release];
Setupviewcontroller = nil;
}
-(Void) dealloc {
[Startbutton release];
[Setupviewcontroller release];
[Super dealloc];
}