IOS 5.0 after Uiviewcontroller NEW: Willmovetoparentviewcontroller and Didmovetoparentviewcon

Source: Internet
Author: User

Before iOS 5.0, we organized the relevant uiview in a Uiviewcontroller

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.

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

@property (nonatomic,readonly) Nsarray *childviewcontrollers

-(void) Addchildviewcontroller: (Uiviewcontroller *) Childcontroller

-(void) Removefromparentviewcontroller

-(void) Transitionfromviewcontroller:::-:::

-(void) Willmovetoparentviewcontroller: (Uiviewcontroller *) Parent

-(void) Didmovetoparentviewcontroller: (Uiviewcontroller *) Parent

In this way, it is possible to control the Uiviewcontroller in a page instead of confusing the common one uiviewcontroller, and most importantly, the revolution in programming habits: reduced the coupling of the function! Write this blog, just say the above 5 methods! That's all. Because when I in Baidu or Google, entered the above 5 methods of the name, found, and did not tell how the 5 methods play a role? How to use? So, I just want to talk about these 5 ways from an API perspective. No more nonsense! Let's get a sense of the concept that is mentioned today:

[Parent View Controller Addchildviewcontroller: child view Controller ];

This Figure Controller Ahas added another Figure Controller B, 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) Durationoptions: (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, 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. At last about the use of the Willmovetoparentviewcontroller method and the Didmovetoparentviewcontroller method

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. namely: [Sub-attempt controller to be removedWillmovetoparentviewcontroller: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 when we call [child View Controller Removefromparentviewcontroller], it is already called by default. You only need to call before the Transitionfromviewcontroller method: [Child View Controller Willmovetoparentviewcontroller:nil].

IOS 5.0 after Uiviewcontroller NEW: Willmovetoparentviewcontroller and Didmovetoparentviewcon

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.