Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change child view controller

I have a view controller that when I press a button a child view controller appear. This works perfectly but I want to change this child view controller for other one if I press the button next that is inside of this to do like two step login. Any idea? Because from the main view controller I know how show a child but from the child I don't know how to do it.

like image 329
Anna Avatar asked Jan 04 '13 14:01

Anna


People also ask

How do I add a child view controller in storyboard?

Add a child view controller programmatically to your contentCall the addChildViewController: method of your container view controller to configure the containment relationship. Add the child's root view to your container's view hierarchy. Add constraints to set the size and position of the child's root view.

What are the benefits of using child view controllers?

A view controller is more self-contained — and can both include the logic required to drive its UI, as well as the UI itself. When added as a child, a view controller automatically fills the screen, reducing the need for additional layout code for full screen UIs.


1 Answers

If using storyboards, you can create your own child replace segue, e.g.:

ReplaceSegue.h

@interface ReplaceSegue : UIStoryboardSegue

@end

ReplaceSegue.m

@implementation ReplaceSegue

- (void)perform
{
    UIViewController *source = self.sourceViewController;
    UIViewController *destination = self.destinationViewController;
    UIViewController *container = source.parentViewController;

    [container addChildViewController:destination];
    destination.view.frame = source.view.frame;
    [source willMoveToParentViewController:nil];

    [container transitionFromViewController:source
                           toViewController:destination
                                   duration:0.5
                                    options:UIViewAnimationOptionTransitionCrossDissolve
                                 animations:^{
                                 }
                                 completion:^(BOOL finished) {
                                     [source removeFromParentViewController];
                                     [destination didMoveToParentViewController:container];
                                 }];
}

@end

You can then open your storyboard, put a container view on your container view controller and on the child controller, put a custom segue from the first child scene to the second child scene. You'll want to specify the ReplaceSegue class for the custom segue between the two child scenes:

container controller storyboard

Note that if you've properly used autolayout or autosizing on the second child view, everything will work fine (especially with the manual setting of the frame of the destination controller's view in the ReplaceSegue). But in Interface Builder, the layout of that second child scene doesn't look quite right and can make the proper design of the scene a little awkward, because (as of Xcode 4.5, at least) the "Simulated Metrics" for the second child scene doesn't do a good job inferring the the proper size. So you should probably change that second child scene's simulated metrics' size from "Inferred" to "Freeform" and then manually adjust the size of the second child scene:

container storyboard with manually adjusted second child scene size

It's not very elegant Xcode experience, but it works. And if you're properly using autolayout or autosizing masks, minor variations are handled very gracefully.

like image 162
Rob Avatar answered Nov 06 '22 13:11

Rob