Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
368 views
Welcome To Ask or Share your Answers For Others

1 Answer

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...