簡體   English   中英

我有多個情節提要。 如何使用AppDelegate在另一個情節提要中打開另一個ViewController? (Segue)

[英]I have multiple storyboards. How can I use AppDelegate to open another ViewController in another storyboard? (Segue)

這是我的代碼。 我嘗試了幾種不同的方法,其中一些方法給我以下錯誤:視圖不在層次結構中。

下面的代碼段輸入正確,否則無法執行segue或presentViewController

func applicationDidTimout(notification: NSNotification) {
    if let vc = self.window?.rootViewController as? UINavigationController {
        if let myTableViewController = vc.visibleViewController as? AccountsOverviewViewController {
            // Call a function defined in your view controller.
            myTableViewController.signOffUser()
        } else {
            // We are not on the main view controller. Here, you could segue to the desired class. 
            let storyboard = UIStoryboard(name: "Accounts", bundle: nil)
            let vc = storyboard.instantiateViewControllerWithIdentifier("AccountsNavigationController") as UIViewController
            let vc2 = getVisibleViewController(nil)
            vc2?.presentViewController(vc, animated: true, completion: nil)

        }
    }
}

func getVisibleViewController(var rootViewController: UIViewController?) -> UIViewController? {

    if rootViewController == nil {
        rootViewController = UIApplication.sharedApplication().keyWindow?.rootViewController
    }

    if rootViewController?.presentedViewController == nil {
        return rootViewController
    }

    if let presented = rootViewController?.presentedViewController {
        if presented.isKindOfClass(UINavigationController) {
            let navigationController = presented as! UINavigationController
            return navigationController.viewControllers.last!
        }

        if presented.isKindOfClass(UITabBarController) {
            let tabBarController = presented as! UITabBarController
            return tabBarController.selectedViewController!
        }

        return getVisibleViewController(presented)
    }
    return nil
}

使用以下功能獲取可見的視圖控制器,

func getVisibleVC() -> UIViewController? {
  if var visibleVC = window?.rootViewController {
    while let presentedVC = visibleVC.presentedViewController {
      visibleVC = presentedVC
    }
    return visibleVC
  }
  return nil
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM