Http://www.1000phone.net/forum.php?mod=viewthread&tid=10507&extra=page%3D1%26filter%3Dauthor%26orderby %3ddateline%26orderby%3ddateline
These two methods are essential to the development of the iphone.
They can all be used to initialize some content when the view is loaded. But what difference do they have?
Viewdidload
This method is called only when view is initialized from the nib file. Viewdidload is used to initialize and load.
Loadview
This method is called when the controller's view is nil. This method is used when creating a view programmatically. Loadview is the method that is called when the view is first loaded when the view is generated using code. Used to implement a control using (write) code. The function used to generate the control using code. 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 when the application is running. If the device is low on memory, the view
The controller receives didreceivememorywarning messages.
The default implementation is to check whether the current controller's view is in use. If its view is not in the view that is currently in use
Hierarchy inside, and your controller implements the Loadview method, the view will be release,
The Loadview method will be re-used to create a new view.
--------------------------------------------------------------------------------------------------------------- -------
Viewdidload, either loading a view from xib or generating a view from Loadview, is called.
--------------------------------------------------------------------------------------------------------------- ------------------------------
Uiviewcontroller Memory Management
After the iOS3.0, Uiviewcontroller a more called
Method of Viewdidunload.
First look at the Uiviewcontroller from the CREATE view
Several functions to the flow of the presentation
-init
-initwithnibname:bundle:
Both of these methods initialize a
VC, but please note that the view
It's not loaded at this time.
-loadview
-viewdidload
When a view is ready to unfold, the VC will first determine
If view has been created, the view is initialized by the previously specified xib file, and other relationships are bound (if no Xib file is specified, the default is to search for the same name as the VC
Xib, such as Mynameviewcontroller will search for mynameviewcontroller.xib files)
If not
Xib file, you can manually create this viewcontrooler required view in Loadview. The next step is to call
-viewdidload, many people like to do something else here, such as making an HTTP request, building an array of what. If this is not handled correctly,-viewdidunload
When activated, the memory is easily disclosed, as mentioned later.
-view () appear
-view () disappear
These methods don't explain.
-viewdidunload
The method receives a memory warning, and the view is not called when the current interface is displayed, at which point the
The controller's view has been freed and is assigned a value of nil.
The next thing you need to do is
Release the child view of the instance variable (iboulet
, as well as the one you added)
Other instance variables, such as an array of data previously in the instance of-viewdidloaded, the HTTP request is freed
Because when the
Viewcontroller is activated again when ready to display (such as Navigationcontroler return to the previous level), VC found its own view
When empty, the previous process is repeated until the view
To create them. If you do not add your own extra child views and the various class instance variables are released, this will be recreated again.
So, the memory leaks out.
--------------------------------------------------------------------------------------------------------------- ----------------------------attention to these two functions Viewwillappear and viewdidappear in iphone development
Both methods are called whenever the view is ready to be displayed on the screen, or when the view is fully displayed on the screen.
Viewwillappear: Method updates the information for the view that is ready to be displayed. When called, the view may not have been loaded yet.
Use Viewdidappear: To trigger the behavior of the view after it is completely displayed on the screen, such as any animation.
Viewdidload is called when the program loads the view for the first time and is not used in the future, and Viewdidappear is called whenever the view is switched.
Loadview and Viewdidload methods. Assuming the view layout is not used with the Xib file, the Loadview method must set the screen and lay out any child views. Whenever you inherit a specific subclass, such as Uitableviewcontroller or Uitabbarcontroller, be sure to call [super
Loadview] or implement Viewdidload. This way, the parent class can set the screen appropriately before making the customization. When the code is based on a specific subclass, Apple's documentation and sample code encourage the use of viewdidload.
Viewdidload
This method is called only when view is initialized from the nib file. Viewdidload is used to initialize and load.
Loadview
This method is called when the controller's view is nil. This method is used when creating a view programmatically. Loadview is the method that is called when the view is first loaded when the view is generated using code. Used to implement a control using (write) code. The function used to generate the control using code.
Viewdidload
, whether you load a view from xib or generate a view from Loadview, it will be called.
--------------------------------------------------------------------------------------------------------------- ---------
Loadview and Viewdidload are the two methods that must be used in the development of the IPhone. They can all be used to initialize some content when the view is loaded.
But what difference do they have?
The Viewdidload method is only available when
The view is called only when it is initialized from the nib file. The viewdidload is used for initialization and loading.
Loadview method in the controller view is nil
is called when the This method is used when creating a view programmatically. Loadview
Is the method that is called when the view is first loaded when the view is generated using code. Used to implement a control using (write) code. The function used to generate the control using code. Such as:
-(void) Loadview {
UIView *view = [[UIView alloc] initwithframe:[uiscreen
Mainscreen]
. Applicationframe];
[View Setbackgroundcolor:_color];
Self.view =
View
[View release];
}
You implemented the Loadview in the controller.
method, you may be called by memory management control at some point when the application is running. If the device is running low on memory, the view controller receives the Didreceivememorywarning
The message. The default implementation is to check whether the current controller's view is in use. If 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.
Nil is meant to be empty. Equivalent to NULL. Nil represents a Objctive-c object with a pointer to null (Nothing is empty).
The first capital of nil and nil is a little different, nil defines a class that points to an empty (class, not an object).
In Objective-c, the nil object is designed to be associated with a NULL null pointer. The difference is that nil is an object, and null is just a value.
In Objective-c, your object is functionally equivalent to a null pointer in many other languages. The difference is that you can invoke the method on nil without causing the program to crash or throw an exception.
--------------------------------------------------------------------------------------------------------------- ------
First, Loadview
Never call this function actively. View
The Controller calls this function when the property of the view is requested and the current view value is nil. If you create a view manually, you should reload the function. If you create a view with IB and initialize the view
Controller, that means you use the Initwithnibname:bundle: method, at which point you should not overload the Loadview function.
The default implementation of this method is to look for information about the available nib files, load the nib file based on this information, and if there is no information about the nib file, the default implementation creates a blank UIView object and then makes the object the controller's main view.
So, when you overload this function, you should do the same. and assign the View property (the view of your create must be a unique instance and not be shared by any other controller), and the function you overload should not call super.
If you want to make further initialization of your views, you should do it in the Viewdidload function. On iOS
3.0 and later, you should overload the Viewdidunload function to release any reference to the view or its contents (sub-view, and so on).
This online information is not comprehensive, especially the blue word part.
Second, Viewdidload
This function is called after the controller has loaded the relevant views, regardless of whether the views are stored in the nib file or generated in the Loadview function. In most cases, it is the follow-up work of nib files.
Third, Viewdidunload
This function is the opposite function of viewdidload. In the absence of program memory, this function is called by the controller to release its view and view-related objects. Since the controller typically holds the view and references to the associated object, you must use this function to discard ownership of these objects for memory recycling. But do not release data that is difficult to reconstruct.
Usually the controller saves a reference to the views created by the nib file, but it may also hold a reference to the object that the Loadview function creates. The perfect method is to use the Synthesizer method: Self.mycertainview = Nil
The synth will release the view, and if you don't use the property, you'll have to show it yourself to release the view.
The description of the function on the net vague, looked at equals not to see.
Iv. Conclusion
So the process should look like this:
(loadview/nib file) to load view to memory
The-->viewdidload function further initializes these view
--when memory is low, call the Viewdidunload function to release views
-When you need to use view, go back to the first step
So loop
--------------------------------------------------------------------------------------------------------------- -------
Viewdidunload method
This method is called when the system memory is tight (note: Viewcontroller is not dealloc)
When memory is tight, didreceivememorywarning is the only way to free up unused memory before iphone OS 3.0, but the OS
3.0 and later Viewdidunload method is a better way
In this method, all Iboutlet (either property or instance variable) are set to nil (System release
The view has been removed.)
--------------------------------------------------------------------------------------------------------------- --------attention to these two functions Viewwillappear and viewdidappear in iphone development
Both methods are called whenever the view is ready to be displayed on the screen, or when the view is fully displayed on the screen.
Viewwillappear: Method updates the information for the view that is ready to be displayed. When called, the view may not have been loaded yet.
Use Viewdidappear: To trigger the behavior of the view after it is completely displayed on the screen, such as any animation.
Loadview and Viewdidload methods. Assuming the view layout is not used with the Xib file, the Loadview method must set the screen and lay out any child views. Whenever you inherit a specific subclass, such as Uitableviewcontroller or Uitabbarcontroller, be sure to call [super
Loadview] or implement Viewdidload. This way, the parent class can set the screen appropriately before making the customization. When the code is based on a specific subclass, Apple's documentation and sample code encourage the use of viewdidload.
These two methods are essential for iphone development (Loadview VS Viewdidload
)。 They can all be used to initialize some content when the view is loaded. But what difference do they have?
Viewdidload
This method is called only when view is initialized from the nib file. Viewdidload is used to initialize and load.
Loadview This method is called when the controller's view is nil.
This method is used when creating a view programmatically.Loadview is the method that is called when the view is first loaded when the view is generated using code. Used to implement a control using (write) code. The function used to generate the control using code.
Viewdidload
, whether you load a view from xib or generate a view from Loadview, it will be called.
--------------------------------------------------------------------------------------------------------------- ------
These two methods are essential to the development of the iphone. They can all be used to initialize some content when the view is loaded.
But what difference do they have?
Viewdidload
This method is called only 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 when the application is running. If the device is low on memory, the view
The controller receives didreceivememorywarning messages. The default implementation is to check whether the current controller's view is in use.
If its view is not inside the view hierarchy currently in use and your controller implements the Loadview method, the view will be release,
The Loadview method will be re-used to create a new view.
--------------------------------------------------------------------------------------------------------------- ---------
Loadview and Viewdidload are IPhone
There are two methods that must be used in development.
They can all be used to initialize some content when the view is loaded. But what difference do they have?
Viewdidload method only when view from NIB
is called when the file is initialized. The viewdidload is used for initialization and loading.
The Loadview method is called when the controller's view is nil. This method is used when creating a view programmatically. Loadview
Is the method that is called when the view is first loaded when the view is generated using code. Used to implement a control using (write) code. The function used to generate the control using code. Such as:
-(void) Loadview {
UIView *view = [[UIView
Alloc] initwithframe:[UIScreen
Mainscreen]. Applicationframe];
[View
Setbackgroundcolor:_color];
Self.view = view;
[View release]
;
}
You implemented the Loadview in the controller.
method, you may be called by memory management control at some point when the application is running. If the device is running low on memory, the view controller receives the Didreceivememorywarning
The message. The default implementation is to check whether the current controller's view is in use. If its view is not in the currently in use view hierarchy
Inside, 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
--------------------------------------------------------------------------------------------------------------- ---------
Viewdidunload method
This method is called when the system memory is tight (note: Viewcontroller is not dealloc)
When memory is tight, didreceivememorywarning is the only way to free up unused memory before iphone OS 3.0, but the OS
3.0 and later the Viewdidunload method is a better way.
--------------------------------------------------------------------------------------------------------------- ---------
From the relationship of Init, Loadview, Viewdidload, Viewdidunload, Dealloc
Init method
Instantiate the necessary objects in the Init method (follow the lazyload idea)
Initialize the Viewcontroller itself in the Init method
Loadview method
Viewcontroller calls this method when the view needs to be displayed and it is nil. Do not call the method directly.
If you manually maintain views, you must override this method by overloading
If you use IB to maintain views, you must not override this method with overloading
Loadview and IB Build view
You implement the Loadview method in the controller, then you may be called by the memory management control at some point when the application is running. If the device is low on memory, the view
The controller receives didreceivememorywarning messages. The default implementation is to check whether the current controller's view is in use. If its view is not in the view that is currently in use
Hierarchy inside, and your controller implements the Loadview method, the view will be release,
The Loadview method will be re-used to create a new view.
Viewdidload method
Viewdidload
This method is called only when view is initialized from the nib file.
Overloads override this method to further customize the view
On iphone OS
In the 3.0 and later versions, you should also overload the rewrite Viewdidunload to release any indexes on the view
Call data Model after Viewdidload
Viewdidunload method
This method is called when the system memory is tight (note: Viewcontroller is not dealloc)
When memory is tight, didreceivememorywarning is the only way to free up unused memory before iphone OS 3.0, but the OS
3.0 and later Viewdidunload method is a better way
In this method, all Iboutlet (either property or instance variable) are set to nil (System release
The view has been removed.)
Frees other view-related objects in this method, other objects created at run time (but not system-required), objects created in viewdidload, cached data, etc.
After the release object, set the object to nil (Iboutlet only need to set it to nil, system release
The view has been removed.)
It is generally assumed that viewdidunload is a viewdidload mirror, because Viewdidload is also re-executed when view is re-requested
The Viewdidunload object must be an object that is easily recreated (such as an object created in Viewdidload or other methods), and do not release user data or other objects that are difficult to re-create
Dealloc method
Viewdidunload and Dealloc methods are not associated, dealloc still do what it has to do.
Article Two:
Viewdidunload
The method receives a memory warning, and the view is not called when the current interface is displayed, and the controller's view
Has been released and is assigned a value of nil.
The next thing you need to do is
Release the child view of the instance variable (iboulet, and add it yourself)
Other instance variables, such as the previous
Data array for instance in-viewdidloaded, HTTP request released
Because when the Viewcontroller is activated again for display (such as
Navigationcontroler return to the previous level), VC found that its own view is empty and will repeat the previous process until the view
To create them. If you do not add your own extra child views and the various class instance variables are released, this will re-create
--------------------------------------------------------------------------------------------------------------- ------
The difference between Loadview and viewdidload
Share
Loadview and Viewdidload are the two methods that must be used in the development of the IPhone.
They can all be used to initialize some content when the view is loaded. But what difference do they have?
Viewdidload method only when view from NIB
is called when the file is initialized. The viewdidload is used for initialization and loading.
The Loadview method is called when the controller's view is nil.
This method is used when creating a view programmatically. Loadview
Is the method that is called when the view is first loaded when the view is generated using code. Used to implement a control using (write) code. The function used to generate the control using code. Such as:
-
(void) Loadview {
UIView * Tmp=[[uiview alloc] Initwithframe:cgrectmake (0,
0, 320, 480)];
Tmp.backgroundcolor=[uicolor
Orangecolor];
self.view=tmp;
[TMP release];
}
You implemented it in the controller.
Loadview method, you may be called by memory management control at some point when the application is running. If the device is low on memory, the view controller receives
Didreceivememorywarning's message. The default implementation is to check whether the current controller's view is in use. If its view is not in the view that is currently in use
Hierarchy inside, and your controller implements the Loadview method, then this view will be release, Loadview
Method will be re-used to create a new view.
--------------------------------------------------------------------------------------------------------------- -------
-(void) viewwillappearbool) animated,-(void) viewdidload
The difference.
Viewwillappear is the code that executes every time the view controller's view appears. And Viewdidload is the code that executes every time the view controller is loaded.
For example, when a view controller views the first occurrence is two to execute, but when a is push after a pop back, only viewwillappear execution.
The difference between Loadview and viewdidload
Share
Loadview and Viewdidload are the two methods that must be used in the development of the IPhone.
They can all be used to initialize some content when the view is loaded. But what difference do they have?
Viewdidload method only when view from NIB
is called when the file is initialized. The viewdidload is used for initialization and loading.
The Loadview method is called when the controller's view is nil.
This method is used when creating a view programmatically. Loadview
Is the method that is called when the view is first loaded when the view is generated using code. Used to implement a control using (write) code. The function used to generate the control using code. Such as:
-
(void) Loadview {
UIView * Tmp=[[uiview alloc] Initwithframe:cgrectmake (0,
0, 320, 480)];
Tmp.backgroundcolor=[uicolor
Orangecolor];
self.view=tmp;
[TMP release];
}
You implemented it in the controller.
Loadview method, you may be called by memory management control at some point when the application is running. If the device is low on memory, the view controller receives
Didreceivememorywarning's message. The default implementation is to check whether the current controller's view is in use. If its view is not in the view that is currently in use
Hierarchy inside, and your controller implements the Loadview method, then this view will be release, Loadview
Method will be re-used to create a new view.
Loadview and Viewdidload Summary of iOS development