动态切换容器视图 [英] Switch container view dynamically

查看:107
本文介绍了动态切换容器视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用情节提要容器视图.所以我有一个包含容器视图的视图控制器.我在情节提要中有第二个较小的视图,该情节通过嵌入segue嵌入到该容器中.到目前为止一切顺利.

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.

现在,我希望能够在用户点击按钮时动态地切换该容器视图的内容.该按钮将在较大的主视图上.我制作了另一个尺寸较小的视图控制器,并为其指定了不同的情节提要ID.

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.

但是,我不知道如何对开关进行编码.我只能为该容器创建一个嵌入的segue.

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.

推荐答案

您需要使用自定义容器视图控制器api进行控制器的切换.以下代码显示了执行此操作的一种方法.在这个项目中,我在主视图控制器(带有容器视图的控制器)中有一个分段控件,该控件在两个控制器之间切换. initialVC是嵌入在IB中的控制器,而replaceVC是我要切换到的控制器.属性container是容器视图的IBOutlet.

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

编辑后:

如果您想要不带动画的新控制器,则可以这样做(这代替了我在情况1下的代码).

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

这篇关于动态切换容器视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆