在 iOS 中切换视图控制器的最佳方式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10265392/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Best way to switch View Controller in iOS
提问by user1191140
I have 2 view controllers in my project. Inside View Controller1 I want to switch to View Controller 2 by press of a button. Currently I do this
我的项目中有 2 个视图控制器。在 View Controller1 中,我想通过按下按钮切换到 View Controller 2。目前我这样做
- (IBAction)startController2:(id)sender {
viewController1 vc2 = [[viewController2 alloc] init];
self.view = vc2.view;
}
This seems to work fine, but there is a big delay (4 secs) between the button press and second view controller appears. If I call the viewController2 directly from the AppDelegate things load faster. What am I doing wrong here. Any help is greatly appreciated.
这似乎工作正常,但按钮按下和第二个视图控制器出现之间有很大的延迟(4 秒)。如果我直接从 AppDelegate 调用 viewController2,则加载速度会更快。我在这里做错了什么。任何帮助是极大的赞赏。
回答by ckhan
Several things to consider.
需要考虑的几件事。
Part 1: "What am I doing wrong here"?
第 1 部分:“我在这里做错了什么”?
You definitely didn't mean to do
self.view = vc2.view
. You just put one view controller in charge of another view controller's view. What you probably mean to say was[self.view addSubview:vc2.view]
. This alone might fix your problem, BUT...Don't actually use that solution. Even though it's almost directly from the samples in some popular iPhone programming books, it's a bad idea. Read "Abusing UIViewControllers"to understand why.
你绝对不是故意的
self.view = vc2.view
。您只需让一个视图控制器负责另一个视图控制器的视图。你可能想说的是[self.view addSubview:vc2.view]
。仅此一项就可以解决您的问题,但是...不要实际使用该解决方案。尽管它几乎直接来自一些流行的 iPhone 编程书籍中的示例,但它是一个坏主意。阅读“滥用 UIViewControllers”以了解原因。
Part 2: What you should be doing
第 2 部分:您应该做什么
It's all in the chapter "Presenting View Controllers from Other View Controllers".
这一切都在“从其他视图控制器呈现视图控制器”一章中。
It'll come down to either:
它会归结为:
a UINavigationController, (see the excellent Apple guide to them here) and then you simply
[navigationController pushViewController:vc2]
a "manually managed" stack of modal view controllers, as andoabhay suggests
explicitly adding a VC as child of another, as jason suggests
一个 UINavigationController,(在这里查看优秀的 Apple 指南)然后你只需
[navigationController pushViewController:vc2]
正如andoabhay所建议的那样,“手动管理”的模态视图控制器堆栈
正如杰森建议的那样,明确添加一个 VC 作为另一个的孩子
回答by Jason
You should consider using UINavigationController
to switch view controllers. If your building target is iOS 5.0+, you can also use the new controller container concept: [mainViewController addChildViewController:childViewController]
.
您应该考虑使用UINavigationController
来切换视图控制器。如果您的构建目标是 iOS 5.0+,您还可以使用新的控制器容器概念:[mainViewController addChildViewController:childViewController]
.
回答by androabhay
Use presentModalViewController
as follows
使用presentModalViewController
如下
[self presentModalViewController:vc2 animated:YES completion:^(void){}];
and in the viewController1
use
并且在viewController1
使用中
[self dismissModalViewControllerAnimated:YES completion:^(void){}];
where ever you want to go back to previous controller.
你想回到以前的控制器的地方。
回答by u7950582
[aController presentViewController:bController animated:NO completion:nil];
[bController presentViewController:cController animated:NO completion:nil];
when you want dismiss cController, you can do like this
当你想解雇 cController 时,你可以这样做
[aController dismissViewControllerAnimated:NO completion:nil];
this is the flow chart.
这是流程图。
aController → bController → cController
↑___________________________↓
回答by Sohail
You should use UINavigationController to switch view controllers.
您应该使用 UINavigationController 来切换视图控制器。
You are on View1 and add the following code on button click method.
您在 View1 上并在按钮单击方法上添加以下代码。
View2 *View2Controller = [[View2 alloc] initWithNibName:@"View2" bundle:nil];
[self.navigationController pushViewController:view2Controller animated:YES];
View2 *View2Controller = [[View2 alloc] initWithNibName:@"View2" bundle:nil];
[self.navigationController pushViewController:view2Controller animated:YES];