I have a Storyboard setup with a UIViewController
with an container view so that I can embed another UIViewController
inside of it.
In a certain scenario I need to change the embedded view controller. In my storyboard I cannot have two segues from my container view (only a single embed segue). Which leads me to doing it programatically.
I have my container view in my storyboard with no connected embed segue.
Now from this point, how can I programmatically embed my chosen UIViewController
object?
An UIViewController is just the base class of this already defined "View Controller", you add a viewController as you said, but a viewController has a class associated to it, that can be a UIViewController, UITableViewController, or variations/subclasses.
To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.
You can do this by programmatically, below is the method which will take a bool value to make decision which view controller need to be added in container view and then will instantiate an object and after that will add it to containerView
- (void)addViewControllerToContainerView:(BOOL)addVC1 { // Get storyboard UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"<name of storyboard>" bundle:[NSBundle mainBundle]]; UIViewController *viewController = nil; if (addVC1) { // get viewController with identifier viewController = [storyBoard instantiateViewControllerWithIdentifier:@"<View Controller 1 Identifier>"]; } else { viewController = [storyBoard instantiateViewControllerWithIdentifier:@"<View Controller 2 Identifier>"]; } // lets add it to container view [viewController willMoveToParentViewController:self]; [self.view addSubview:viewController.view]; [self addChildViewController:viewController]; [viewController didMoveToParentViewController:self]; // keep reference of viewController which may be useful when you need to remove it from container view, lets consider you have a property name as containerViewController self.containerViewController = viewController; }
When you need remove view controller from container view controller you can do this
[self.containerViewController willMoveToParentViewController:nil]; // 1 self.containerViewController.view removeFromSuperView]; [self.containerViewController removeFromParentViewController];//this line is updated as view is removed from parent view cotnroller istead of its viewcontroller is removed from parentViewController self.containerViewController = nil
Apple docs about container view controllers
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With