问题描述
我的项目中有2个视图控制器。 Inside View Controller1我想通过按下按钮切换到View Controller 2。目前我这样做
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
- (IBAction)startController2:(id)sender {
viewController1 vc2 = [[viewController2 alloc] init];
self.view = vc2.view;
}
这似乎工作正常,但有很大的延迟(4秒)按钮按下和第二视图控制器之间出现。如果我直接从AppDelegate调用viewController2加载更快的东西。我在这做错了什么非常感谢任何帮助。
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.
推荐答案
需要考虑的几件事情。
Several things to consider.
-
你绝对不想做
self.view = vc2.view
。您只需将一个视图控制器负责另一个视图控制器的视图。你可能想说的是[self.view addSubview:vc2.view]
。仅此一项可能会解决您的问题,但 ...
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.
第2部分:你应该做什么
这一切都在章。
它将归结为:
-
UINavigationController ,(请参阅优秀的Apple指南)然后你只需
[navigationController pushViewController:vc2]
一个手动管理的模态视图控制器堆栈,如
a "manually managed" stack of modal view controllers, as andoabhay suggests
明确添加VC作为另一个人的孩子,如
explicitly adding a VC as child of another, as jason suggests
这篇关于在iOS中切换View Controller的最佳方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!