Apple adds a new 5 method and a property to Uiviewcontroller in IOS5:
Removefromparentviewcontroller:
Didmovetoparentviewcontroller:
Property
@property (nonatomic,nsarray *childviewcontrollers
The original question
These new methods and properties are used to improve our way of programming. So let's take a look at some of the previous potential problems with the use of Uiviewcontroller and recognize the problem, so that we can understand the meaning of Apple's change.
In the past, a Uiviewcontroller view might have a lot of small sub-view. These sub-view are often covered at the end, and we add a lot of sub-view with Addsubview in the outermost Viewcontroller Viewdidload method. Most of these sub-view will not always be on the interface, only in some cases will appear, such as the login failed prompt view, upload attachment successful hint view, network failure prompt view and so on. But while these are rarely seen, we often keep them in memory. In addition, when a memory warning is received, we can only manually remove these view from Super view.
Change
Apple's new API adds the Addchildviewcontroller method and wants us to call the [self Addchildviewcontroller:child] method when using Addsubview, which corresponds to the VI of Sub view Ewcontroller is also added to the current Viewcontroller management. For those who currently do not need to display the Subview, only through the Addchildviewcontroller Subviewcontroller add in. Call the TransitionFromViewController:toViewController:duration:options:animations:completion method when it needs to be displayed.
In addition, when the memory Warning of the system is received, the system will automatically unload out the subview that are not currently displayed to save RAM.
Let's get a sense of the concept that is mentioned today:
[ Parent View Controller Addchildviewcontroller: child view Controller ];
Here, figure Controller A adds another diagram controller B, then a acts as the parent view Controller and B acts as a child view controller. The parent View Controller acts as a role for the View controller container.
Addchildviewcontroller Method:
-(void) Addchildviewcontroller: (Uiviewcontroller *)Childcontroller
To add a child view controller to the View Controller container
Childcontroller: Child View Controller
When the child view controller that you want to add is already included in the View controller container, it is equivalent to first being removed from the parent view controller and then re-added to the parent view controller.
Removefromparentviewcontroller Method
-(void) Removefromparentviewcontroller
Removed from the parent view controller.
Transitionfromviewcontroller Method
-(void) Transitionfromviewcontroller: (Uiviewcontroller *)Fromviewcontrollertoviewcontroller: ( Uiviewcontroller *)toviewcontroller Duration: (nstimeinterval)duration options: ( uiviewanimationoptions)options animations: (void (^) (void))animations completion: (void (^) (BOOL finished)) completion
Swap the locations of two child view controllers (because the order of the additions is different, the child tries to have a hierarchical relationship in the parent view Controller)
Fromviewcontroller: The currently displayed child tries the controller and will be replaced with a non-display state
Toviewcontroller: The child View controller that will be displayed
Duration: Swap animation duration, per second
Options: How to animate
Animations: Animation block
Completion: block to be executed after completion
Willmovetoparentviewcontroller Method
-(void) Willmovetoparentviewcontroller: (Uiviewcontroller *)parent
The method is called when a view controller is added to or removed from the View controller container.
Parent: Parental view Controller, if no parent view controller, will be nil
Note the point:
1. When we call the Removefromparentviewcontroller method to our view controller container, we must first call the method, and the parent parameter is nil:
[ View controller to be removed Willmovetoparentviewcontroller:nil];
2. When we call the Addchildviewcontroller method, the method is called automatically before the child view controller is added. So, there is no need for us to display the call.
Didmovetoparentviewcontroller Method
-(void) Didmovetoparentviewcontroller: (Uiviewcontroller *)parent
This method is called when Viewcontroller is added from a View control container or removed .
Parent: Parental view Controller, if no parent view controller, will be nil
When we add (or remove) a child view controller to our view Controller container (which is the parent view controller, which calls the Addchildviewcontroller method to join the child view controller, it becomes the container of the view controller), you must call the method to tell IOS, has finished adding (or removing) the sub-controller operations.
The Removefromparentviewcontroller method automatically calls the method, so after you delete the child controller, you do not need to call the method in the display.
In fact, these several methods of the API description, see also understand.
At last
about the Willmovetoparentviewcontroller methods and The use of Didmovetoparentviewcontroller methods
1. These two methods are called when the child tries to switch the controller! Called when the Transitionfromviewcontroller method is called.
2. when calling the Willmovetoparentviewcontroller method or The Didmovetoparentviewcontroller method, be aware that their arguments are used:
When a child view controller is removed from the parent view controller, the parent parameter is nil.
That is: [ the sub-attempt controller to be deleted Willmovetoparentviewcontroller:nil];
When a child tries to join the controller to the parent view controller, the parent parameter is the parental view controller.
That is: [The child view controller to be joined Didmovetoparentviewcontroller: parent View Controller ];
3. No need to invoke the [ Child view Controller Willmovetoparentviewcontroller: parent View Controller ] method. Because when we call the [parent View Controller addchildviewcontroller: child View Controller ], it is already called by default.
You only need to call the Child View Controller Didmovetoparentviewcontroller: parent View Controller after the Transitionfromviewcontroller method;
4. No need to invoke the [ Child view Controller Didmovetoparentviewcontroller: parent View Controller ] method. Because we call
[ Child View Controller Removefromparentviewcontroller], has been called by default.
You only need to call before the Transitionfromviewcontroller method: [ child View Controller Willmovetoparentviewcontroller:Nil].
The new method in IOS5