Use of IOS Willmovetoparentviewcontroller and Didmovetoparentviewcontroller

Source: Internet
Author: User

On iOS 5.0 and later, iOS added new properties and methods to the Uiviewcontroller class:

123456 @property (nonatomic,readonly)   Nsarray *childviewcontrollers -  ( void -  ( void ) Removefromparentviewcontroller -  ( void -  ( void ) Willmovetoparentviewcontroller: ( uiviewcontroller *) Parent -  ( void

In this way, you can control the Uiviewcontroller in a page instead of confusing the common one uiviewcontroller, and most importantly, the revolution in programming habits: reducing the coupling of functionality!

Here's a look at the use of these several methods:

First, [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.

1, Addchildviewcontroller Method:

1 - (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.

2. Removefromparentviewcontroller method

1 - (void)removeFromParentViewController

Removed from the parent view controller.

3. Transitionfromviewcontroller method

1 - (void)transitionFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController duration:(NSTimeInterval)duration options:(UIViewAnimationOptions)options animations:(void(^)(void))animations completion:(void (^)(BOOLfinished))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

4. Willmovetoparentviewcontroller method

1 - (void)willMoveToParentViewController:(UIViewController *)parent

When a view controller is added to or removed from the View controller container, the method is called Parent: the parental view controller, and nil if there is no parent view Controller

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.

5. Didmovetoparentviewcontroller method

1 - (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, tell iOS, The operation to add (or remove) the child controller has been completed.

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.

Ii. use of Willmovetoparentviewcontroller methods and 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].

Use of IOS Willmovetoparentviewcontroller and Didmovetoparentviewcontroller

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.