2

I'd like to swap two custom UIViewControllers inside a ContainerView. To do this I created a ViewController that handles the logic of the swapping. This swap should occur when pressing a button.

What I want to do is similar to this. I tried the current accepted answer but the problem is that the layout doesn't show up. I don't get any compile errors or warnings.

I have this code in the ContainerViewController, executed when the button is pressed.

if([view isEqualToString:@"view1"]){
    NSString *userId = [self getUserId];

    ViewController1 *vc = [[ViewController1 alloc]init];
    [vc someFunction:userId];
    UIViewController *fromViewController = [self.childViewControllers firstObject];
    [self swapViewController: fromViewController toViewController:vc];

}else{
    NSString *userId = [self getUserId];

    ViewController2 *vc = [[ViewController2 alloc]init];
    [vc otherFunction:userId];
    UIViewController *fromViewController = [self.childViewControllers firstObject];
    [self swapViewController: fromViewController toViewController:vc];
}

The swapViewController function looks like this:

- (void)swapViewController:(UIViewController *)fromViewController toViewController:(UIViewController *)toViewController
{
    toViewController.view.frame = fromViewController.view.frame;
    [fromViewController willMoveToParentViewController:nil];
    [self addChildViewController:toViewController];

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

This is what I have in the storyboard:

enter image description here

If I just simply load the segue it works fine, but I need to show some data received over the internet, so I think it's not an option.

Could anyone please, give me directions on how to achieve that?

Community
  • 1
  • 1
angudu
  • 183
  • 1
  • 2
  • 6

0 Answers0