我有多个故事板。如何使用AppDelegate在另一个故事板中打开另一个ViewController? (Segue公司)

时间:2016-04-15 12:22:57

标签: ios swift appdelegate

这是我的代码。我尝试了一些不同的方法,其中一些方法给我的错误是视图不在层次结构中。

下面的代码段输入正确的其他内容,但无法执行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
}

1 个答案:

答案 0 :(得分:1)

使用下面的func获取可见的视图控制器

xcrun simctl erase all
相关问题