一尘不染

通过App Delegate快速执行Segue

swift

我需要从应用程序委托启动视图控制器。

以这种方式,您将在视图控制器之间执行排序。

我有一个if语句,如果为true需要显示视图控制器,则在应用程序委托中。

如何从应用程序委托执行此操作?


阅读 184

收藏
2020-07-07

共1个答案

一尘不染

c_rath的答案基本上是正确的,但是您无需将视图控制器设为根视图控制器。实际上,即使从App
Delegate,您也可以在导航堆栈的顶视图和任何其他视图控制器之间触发选择。例如,要推送情节提要视图控制器,您可以执行以下操作:

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)
2020-07-07