问题描述
我需要从app delegate启动一个视图控制器。
I need to launch a view controller from the app delegate.
在视图控制器之间执行segue的方式。
In the way you would perform a segue between view controllers.
我有一个if语句,如果true需要显示一个视图控制器,这是在app delegate中。
I have an if statement that if true needs to show a view controller, this is in the app delegate.
我如何从app委托?
推荐答案
c_rath的答案大多是正确的,但你不需要让视图控制器成为根视图控制器。实际上,您甚至可以从App Delegate触发导航堆栈顶视图和任何其他视图控制器之间的segue。例如,要推送故事板视图控制器,您可以执行以下操作:
c_rath's answer is mostly right, but you don't need to make the view controller a root view controller. You can in fact trigger a segue between the top view on the navigation stack and any other view controller, even from the App Delegate. For example, to push a storyboard view controller, you could do this:
Swift 3.0及更高版本
// Access the storyboard and fetch an instance of the view controller
let storyboard = UIStoryboard(name: "Main", bundle: nil);
let viewController: MainViewController = storyboard.instantiateViewController(withIdentifier: "ViewController") as! MainViewController;
// Then push that view controller onto the navigation stack
let rootViewController = self.window!.rootViewController as! UINavigationController;
rootViewController.pushViewController(viewController, animated: true);
Swift 2.0及早期
// Access the storyboard and fetch an instance of the view controller
var storyboard = UIStoryboard(name: "Main", bundle: nil)
var viewController: MainViewController = storyboard.instantiateViewControllerWithIdentifier("ViewController") as MainViewController
// Then push that view controller onto the navigation stack
var rootViewController = self.window!.rootViewController as UINavigationController
rootViewController.pushViewController(viewController, animated: true)
这篇关于从App Delegate swift执行Segue的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!