[英]Best way to switch View Controller in iOS
I have 2 view controllers in my project. 我的项目中有2个视图控制器。 Inside View Controller1 I want to switch to View Controller 2 by press of a button. Inside View Controller1我想通过按下按钮切换到View Controller 2。 Currently I do this 目前我这样做
- (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. 这似乎工作正常,但按钮按下和第二个视图控制器之间出现了很大的延迟(4秒)。 If I call the viewController2 directly from the AppDelegate things load faster. 如果我直接从AppDelegate调用viewController2加载更快的东西。 What am I doing wrong here. 我在这做错了什么。 Any help is greatly appreciated. 任何帮助是极大的赞赏。
Several things to consider. 需要考虑的几件事情。
You definitely didn't mean to do self.view = vc2.view
. 你绝对不是故意做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]
. 你可能想说的是[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. 虽然它几乎直接来自一些流行的iPhone编程书中的样本,但这是一个坏主意。 Read "Abusing UIViewControllers" to understand why. 阅读“滥用UIViewControllers”以了解原因。
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]
一个UINavigationController,(看到优秀的苹果直营店他们在这里 ),然后你只需[navigationController pushViewController:vc2]
a "manually managed" stack of modal view controllers, as andoabhay suggests 正如andoabhay建议的那样 ,一个“手动管理”的模态视图控制器堆栈
explicitly adding a VC as child of another, as jason suggests 正如杰森所说 ,明确地将VC作为另一个孩子的孩子
You should consider using UINavigationController
to switch view controllers. 您应该考虑使用UINavigationController
来切换视图控制器。 If your building target is iOS 5.0+, you can also use the new controller container concept: [mainViewController addChildViewController:childViewController]
. 如果你的构建目标是iOS 5.0+,你也可以使用新的控制器容器概念: [mainViewController addChildViewController:childViewController]
。
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. 你想在哪里回到以前的控制器。
[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
↑___________________________↓
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];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.