如何检查具有ID的Navigation Controller是否存在,iOS Swift

时间:2017-09-28 15:16:25

标签: ios swift uiviewcontroller uinavigationcontroller uiwindow

我想检查UIApplication.shared.windows

中是否存在带有restorationId(在故事板中定义)的NavigationController

如果是,我想检查一个带有ID的ViewController是否在该NavigationController上。如果是,请将其置于堆栈顶部,如果没有创建viewController的实例并将其添加到导航堆栈中。

这就是我试图实现上述目标的方法,但我无法超越窗口打印。

有人可以建议我哪里出错吗?

func identifyAndPresentViewController(){

    let windows = UIApplication.shared.windows
    print("windows: \(windows)")

    for window in windows{
        print("window: \(window)")

        if let nav     = window.rootViewController as? UINavigationController {

            print("nav: \(nav)")

            let navId   = nav.restorationIdentifier
            print("navId: \(String(describing: navId))")

            if navId == "VideoCallTVCNav" {

                print("navId matches")

                let vcs = nav.viewControllers

                print("vcs: \(vcs)")

                for vc in vcs{

                    print("vc: \(vc)")

                    if vc.restorationIdentifier == "VideoCallTVC"{

                        // Display VideoCallTVC inside nav with restorationId "VideoCallTVCNav"

                        print("VideoCallTVC exists")

                    }else{

                        // Create a instance of "VideoCallTVC" and display it inside nav with restorationId "VideoCallTVCNav"

                        print("VideoCallTVC does not exists")
                    }
                }
            }
        }

    }
}

0 个答案:

没有答案