Perform Segue from App Delegate swift

前端 未结 7 1032
野的像风
野的像风 2020-12-06 00:53

I need to launch a view controller from the app delegate.

In the way you would perform a segue between view controllers.

I have an if statement that if true

相关标签:
7条回答
  • 2020-12-06 01:09

    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 and Later

    // 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 and Earlier

    // 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)
    
    0 讨论(0)
  • 2020-12-06 01:11

    if you want to perform segue from AppDelegate. Implement segue between ViewControllers in your storyboard. Give identifier to your segue.

    self.window?.rootViewController!.performSegue(withIdentifier: "your_identifier", sender: nil)
    
    0 讨论(0)
  • 2020-12-06 01:17

    You don't need to instantiate new ViewController, just perform segue from presentedViewController like this (swift 3.0+)

    guard let mainController = self.window?.rootViewController?.presentedViewController as? MainViewController else { return }
    mainController.performSegue(withIdentifier: "SEGUE_ID", sender: nil)
    

    In case when your application is based on UITabBarController, you must:

    • fetch UITabBarController
    • select proper controller from viewControllers collection from UITabBarController
    • change index of UITabBarController
    • lastly you can perform segue

    Of course you must know the index of your view controller in your UITabBarController

    Example code:

    guard let tabBarController = self.window?.rootViewController?.presentedViewController as? UITabBarController else { return }
    guard let tabBarViewControllers = tabBarController.viewControllers else { return }
    guard let mainViewController = tabBarViewControllers[0] as? MainViewController else { return }
    tabBarController.selectedIndex = 0
    mainViewController(withIdentifier: "SEGUE_ID", sender: nil)
    
    0 讨论(0)
  • 2020-12-06 01:19

    If you want segue in 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
        }
    

    Note if you not use DispatchQueue.main.async than your segue is not perform

    0 讨论(0)
  • 2020-12-06 01:24

    if you want to use segue identifier, you can use in Swift 2.2.

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

    and for Swift 3.1 :

    self.window?.rootViewController!.performSegue(withIdentifier: "YOUR SEGUE IDENTIFIER", sender: nil)
    
    0 讨论(0)
  • 2020-12-06 01:25

    You can't actually perform a segue from the AppDelegate since a segue is a transition from one scene to another. One thing you can do however is instantiate a new view controller and present it from the AppDelegate. Something similar to the following should work...

    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
    }
    
    0 讨论(0)
提交回复
热议问题