Created
November 13, 2012 23:30
-
-
Save mluton/4069137 to your computer and use it in GitHub Desktop.
Manual Management of Child View Controllers with Storyboard Segues
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #import "ContainerViewController.h" | |
| #define SegueIdentifierFirst @"embedFirst" | |
| #define SegueIdentifierSecond @"embedSecond" | |
| @interface ContainerViewController () | |
| @property (strong, nonatomic) NSString *currentSegueIdentifier; | |
| @end | |
| @implementation ContainerViewController | |
| - (void)viewDidLoad | |
| { | |
| [super viewDidLoad]; | |
| self.currentSegueIdentifier = SegueIdentifierFirst; | |
| [self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil]; | |
| } | |
| - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender | |
| { | |
| if ([segue.identifier isEqualToString:SegueIdentifierFirst]) | |
| { | |
| if (self.childViewControllers.count > 0) { | |
| [self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:segue.destinationViewController]; | |
| } | |
| else { | |
| [self addChildViewController:segue.destinationViewController]; | |
| ((UIViewController *)segue.destinationViewController).view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); | |
| [self.view addSubview:((UIViewController *)segue.destinationViewController).view]; | |
| [segue.destinationViewController didMoveToParentViewController:self]; | |
| } | |
| } | |
| else if ([segue.identifier isEqualToString:SegueIdentifierSecond]) | |
| { | |
| [self swapFromViewController:[self.childViewControllers objectAtIndex:0] toViewController:segue.destinationViewController]; | |
| } | |
| } | |
| - (void)swapFromViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController | |
| { | |
| toViewController.view.frame = CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height); | |
| [fromViewController willMoveToParentViewController:nil]; | |
| [self addChildViewController:toViewController]; | |
| [self transitionFromViewController:fromViewController toViewController:toViewController duration:1.0 options:UIViewAnimationOptionTransitionCrossDissolve animations:nil completion:^(BOOL finished) { | |
| [fromViewController removeFromParentViewController]; | |
| [toViewController didMoveToParentViewController:self]; | |
| }]; | |
| } | |
| - (void)swapViewControllers | |
| { | |
| self.currentSegueIdentifier = (self.currentSegueIdentifier == SegueIdentifierFirst) ? SegueIdentifierSecond : SegueIdentifierFirst; | |
| [self performSegueWithIdentifier:self.currentSegueIdentifier sender:nil]; | |
| } | |
| @end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment