我需要从app delegate启动一个视图控制器.
在您在视图控制器之间执行segue的方式.
我有一个if语句,如果true需要显示一个视图控制器,这是在app委托中.
我如何从应用代表执行此操作?
Lyn*_*ott 36
c_rath的答案大多是正确的,但您不需要将视图控制器设置为根视图控制器.实际上,您甚至可以从App Delegate触发导航堆栈顶视图和任何其他视图控制器之间的segue.例如,要推送故事板视图控制器,您可以执行以下操作:
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);
Run Code Online (Sandbox Code Playgroud)
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)
Run Code Online (Sandbox Code Playgroud)
小智 10
如果你想使用segue
identifier
,你可以使用Swift
2.2
.
self.window?.rootViewController!.performSegueWithIdentifier("YOUR SEGUE IDENTIFIER", sender: nil)
Run Code Online (Sandbox Code Playgroud)
对于Swift 3.1:
self.window?.rootViewController!.performSegue(withIdentifier: "YOUR SEGUE IDENTIFIER", sender: nil)
Run Code Online (Sandbox Code Playgroud)