I have a UIViewController with a UIContainerView inside. Based on whether a certain condition is true, I'd like to programmatically set the container view's embedded view to a different UIViewController. I noticed that you can have only one embed segue to set one UIViewController, so is there a way to get this done?
I tried setting my container view as an outlet, but I could not find any methods that set an embedded UIViewController. Any advice on how to get started with this would be appreciated.
I think your idea is wrong if you meant swapping UIViews of UIViewController (hope I understood your conception).
A UIViewController should have 1 designed UIView and should manage the values of that view. As you said, you can use Containers, however you should add then UIViewController with it's view, so there is an instance which manages this view. Your first UIViewController should only add/remove that ChildViewController.
So I'd advise:
Implement a category on UIViewController and add following methods:
- (void)displayContentController:(UIViewController *)content {
[self addChildViewController:content];
content.view.frame = [[UIScreen mainScreen] bounds];
[self.view addSubview:content.view];
[content didMoveToParentViewController:self];
}
- (void)hideContentController:(UIViewController *)content {
[content willMoveToParentViewController:nil];
[content.view removeFromSuperview];
[content removeFromParentViewController];
}
Create AViewController and BViewController. In AViewController call (viewDidLoad?):
BViewController *bViewController = [[BViewController alloc] init];
[self displayContentController:bViewController];
In BViewController manage the view of this controller. AViewController should only manage when to show BViewController and when to hide it.
If I misunderstood your question please comment it, I'll delete this answer.
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