如何在iPhone上已经存在的UIViewController之上添加一个新的UIViewController是最佳方法?我知道两种方法。但是是否有更好的方法?如果没有,那么这两种方法中哪一种更好呢?
1. [self presentModalViewController:controller animated:NO];
2. [self.view addSubview:someController.view];
如何在iPhone上已经存在的UIViewController之上添加一个新的UIViewController是最佳方法?我知道两种方法。但是是否有更好的方法?如果没有,那么这两种方法中哪一种更好呢?
1. [self presentModalViewController:controller animated:NO];
2. [self.view addSubview:someController.view];
这取决于你想要如何实现。如果你想使用现有的转换来显示和解除视图控制器,可以使用 presentModalViewController。但是,如果你想用一些自定义动画来显示它,可以使用 addSubView。再次强调,完全取决于你。
这取决于您显示视图控制器的要求。可能会有一个或多个控制器推到导航堆栈中。
[self.navigationController pushViewController:anotherViewController animated:YES];
查看应用程序论坛帖子,了解何时使用pushViewController:
和何时使用presentModalViewController:
。
[[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) {
}
];
这两种方式几乎等效,因为它们都使用视图堆栈。你可以通过 [self removeFromSuperview]
来看到下面的视图(直到未被碱化),当你使用 addSubView
时;而当你使用 [self presentModalViewController:tempView animated:NO];
时,可以通过 [self dismissModalViewControllerAnimated:YES];
进行操作。但是,presentModalViewController
提供了默认的动画选项,而使用 addSubview
需要进行一些调整。
这取决于你想要什么。没有一种方法比另一种更好。所有的方法都只是...方法。