Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch container view dynamically

Tags:

I'm using a storyboard container view. So I have a view controller containing a container view. I have a second, smaller view in the storyboard, which is embedded in that container via an embed segue. So far so good.

Now I would like to be able to switch the contents of that container view dynamically, when the user taps a button. The button will be on the main, larger, view. I have made another view controller of the same smaller size, and given it a different storyboard ID.

However, I can't work out how to code the switch. I can only create a single embed segue for that container.

Any help gratefully received.

like image 612
James White Avatar asked Apr 17 '13 05:04

James White


1 Answers

You need to use the custom container view controller api to do the switching of the controllers. The following code shows one way to do that. In this project I had a segmented control in the main view controller (the one with the container view) that switches beetween the two controllers. initialVC is the controller that's embedded in IB, and substituteVC is the one I'm switching to. The property, container, is an IBOutlet to the container view.

- (void)viewDidLoad {     [super viewDidLoad];     self.initialVC = self.childViewControllers.lastObject;     self.substituteVC = [self.storyboard instantiateViewControllerWithIdentifier:@"Substitute"];     self.currentVC = self.initialVC; }  -(IBAction)switchControllers:(UISegmentedControl *)sender {      switch (sender.selectedSegmentIndex) {         case 0:             if (self.currentVC == self.substituteVC) {                 [self addChildViewController:self.initialVC];                 self.initialVC.view.frame = self.container.bounds;                 [self moveToNewController:self.initialVC];             }             break;         case 1:             if (self.currentVC == self.initialVC) {                 [self addChildViewController:self.substituteVC];                 self.substituteVC.view.frame = self.container.bounds;                 [self moveToNewController:self.substituteVC];             }             break;         default:             break;     } }   -(void)moveToNewController:(UIViewController *) newController {     [self.currentVC willMoveToParentViewController:nil];     [self transitionFromViewController:self.currentVC toViewController:newController duration:.6 options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil                             completion:^(BOOL finished) {                                 [self.currentVC removeFromParentViewController];                                 [newController didMoveToParentViewController:self];                                 self.currentVC = newController;                             }]; } 

After Edit:

If you want to go to the new controller with no animation, you can do it like this (this substitutes for the code that I had under case 1).

[self addChildViewController:self.substituteVC]; [self.substituteVC didMoveToParentViewController:self]; self.substituteVC.view.frame = self.container.bounds; [self.container addSubview:self.substituteVC.view]; [self.currentVC removeFromParentViewController]; 
like image 57
rdelmar Avatar answered Oct 02 '22 15:10

rdelmar