1

I have a MainViewController which has a ContainerView inside, it shows ViewControllerA.

Is there a method to go from UIViewControllerA to UIViewControllerB in the ContainerView?

Hierarchy is:

MainViewController -> containerView
ViewControllerA    -> btnShowViewControllerB
ViewControllerB

Codes are:

// MainViewController.m

- (void)viewDidLoad {
[super viewDidLoad];

    // Show ViewControllerA in the ContainerView programmatically
    ViewControllerA *vcA = [self.storyboard instantiateViewControllerWithIdentifier:@"A"];
    [self addChildViewController:vcA];
    [self.containerView addSubview:vcA.view];
    [vcA didMoveToParentViewController:self];
}

// ViewControllerA.m

- (IBAction)btnShowViewControllerB:(UIButton *)sender {
}

Thank you

Esra Karakecili
  • 109
  • 1
  • 9

2 Answers2

0

This is how you can switch viewControllers programmatically:

[self addChildViewController:nextViewController];
nextViewController.view.frame = view.bounds;
[view addSubview:nextViewController.view];
[nextViewController didMoveToParentViewController:self];
Michael
  • 1,030
  • 14
  • 29
0

You need to make the transition call in the container view controller. If the button action is inside ViewControllerA, then you need a way to tell the container view controller, "Hey - it's time to swap view controllers now". Typically this is done with a delegate. This question has the details on how to setup and use a delegate. Once your container knows to make the swap, you can animate it using a call like this inside the container view controller:

[self addChildViewController:viewControllerB];
[self transitionFromViewController:viewControllerA
                  toViewController:viewControllerB
                          duration:0.3
                           options:UIViewAnimationOptionCurveEaseIn
                        animations:^{
                            viewControllerB.view.alpha = 1;
                            viewControllerA.view.alpha = 0;
                        } completion:^(BOOL finished) {
                            [viewControllerA removeFromParentViewController];
                        }];
Community
  • 1
  • 1
John Grant
  • 1,679
  • 10
  • 8