在容器视图中交换视图

时间:2014-12-23 17:45:31

标签: ios objective-c uistoryboard

我想在UIViewController内交换两个自定义ContainerView。为此,我创建了一个ViewController来处理交换的逻辑。按下按钮时会发生此交换。

我想要做的是与this类似。我尝试了当前接受的答案,但问题是布局没有显示出来。我没有收到任何编译错误或警告。

我在ContainerViewController中有这个代码,在按下按钮时执行。

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

swapViewController函数如下所示:

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

这就是我在故事板中的内容:

enter image description here

如果我只是加载segue它工作正常,但我需要显示一些通过互联网收到的数据,所以我认为它不是一个选项。

有人可以,请告诉我如何实现这个目标?

0 个答案:

没有答案