在另一个UIViewController之上添加UIViewController的最佳方法

时间:2011-07-02 03:52:22

标签: iphone objective-c ios ipad

在我的iPhone上已经存在的UIViewController之上添加UIViewController的最佳方法是什么。我知道有两种方法。但有更好的方法吗?如果没有,哪一个更好?

1. [self presentModalViewController:controller animated:NO];
2. [self.view addSubview:someController.view];

5 个答案:

答案 0 :(得分:2)

这取决于您希望如何实现它。如果要显示视图控制器并使用现有转换关闭它,可以使用 presentModalViewController 。但是,如果要使用某些自定义动画显示它,可以使用 addSubView 。而且,它完全取决于你。

答案 1 :(得分:1)

这取决于您显示视图控制器的要求。 可能还有一个人在导航堆栈中推动控制器。

[self.navigationController pushViewController:anotherViewController animated:YES];

检查appl论坛帖子,了解何时使用pushViewController:以及presentModalViewController:

pushViewController versus presentModalViewController

presentModalViewController vs. pushViewController

答案 2 :(得分:1)

[[self view] addSubview: [otherViewController view]];

CGRect frame = [[self view] frame];

int direction = 1;
switch (direction) {
    case 0: // from bottom
        frame.origin.y = [[self view] frame].size.height;
        [[otherViewController view] setFrame: frame];
        frame.origin.y = 0.0 - [[self view] frame].size.height;
        break;
    case 1: // from right
        frame.origin.x = [[self view] frame].size.width;
        [[otherViewController view] setFrame: frame];
        frame.origin.x = 0.0 - [[self view] frame].size.width;
        break;
}

[UIView animateWithDuration: 1.0
                      delay: 0.0
                    options: UIViewAnimationOptionCurveEaseInOut
                 animations:^{
                     [[self view] setFrame: frame];
                 }
                 completion: ^(BOOL finished) {

                 }
];

答案 3 :(得分:0)

两种方式几乎相同,就像制作一堆视图一样,当你[self removeFromSuperview]addSubView时,你可以看到[self dismissModalViewControllerAnimated:YES];下面的视图(直到不是碱性)。 {1}},当您使用[self presentModalViewController:tempView animated:NO];时,但是,presentModalViewController,无论是addSubview还是动态,都为动画提供默认选项,您需要为此工作一点。

答案 4 :(得分:-1)

取决于你想要什么。没有一种方法比另一方更好。一切都只是......方式。

相关问题