从应用程序代表快速执行Segue

我需要从应用程序代理启动一个视图控制器。

在视图控制器之间执行segue的方式。

我有一个if语句,如果真的需要显示一个视图控制器,这是在应用程序委托。

我如何从应用程序委托做到这一点?

c_rath的答案大部分是正确的,但是你不需要把视图控制器变成一个根视图控制器。 实际上,您可以在导航堆栈的顶视图和任何其他视图控制器之间触发一个segue,即使是从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) 

如果你想使用segue identifier ,你可以在Swift 2.2使用。

 self.window?.rootViewController!.performSegueWithIdentifier("YOUR SEGUE IDENTIFIER", sender: nil) 

和Swift 3.1:

 self.window?.rootViewController!.performSegue(withIdentifier: "YOUR SEGUE IDENTIFIER", sender: nil) 

如果您的rootViewController没有从UINavigationViewControllerinheritance,那么您rootViewController

基本上你检查当前提供的标签是什么,然后从THAT视图控制器,而不是从UITabBarControllerinheritance的rootViewController

  let root = self.window?.rootViewController as UITabBarController switch(root.selectedIndex){ case 0: root.viewControllers?[0].pushViewController(viewController, animated: true) break; case 1: root.viewControllers?[1].pushViewController(viewController, animated: true) break default: println("Error presenting ViewController") break; } 

我的应用程序有一个从UITabBarControllerinheritance的初始/ rootViewController,它的每个UITabBar关系控制器从UINavigationControllerinheritance,允许我从它们pushViewController

由于segue是从一个场景到另一个场景的过渡,所以你不能从AppDelegate执行一个segue。 你可以做的一件事是实例化一个新的视图控制器,并从AppDelegate展示它。 类似下面的东西应该工作…

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool { // Override point for customization after application launch. var storyboard = UIStoryboard(name: "Main", bundle: nil) var viewController: MasterViewController = storyboard.instantiateViewControllerWithIdentifier("viewController") as MasterViewController window?.rootViewController = viewController window?.makeKeyAndVisible() return true } 

如果你想在App Delegate Swift 3.0中继续

  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool { if UserDefaults.standard.bool(forKey: PARAM.STATUS) { DispatchQueue.main.async { self.window?.rootViewController?.performSegue(withIdentifier: PARAM.SEGUETOHOME, sender: nil) } } // Override point for customization after application launch. return true } 

请注意,如果您不使用DispatchQueue.main.async ,则不会执行segue