DismissViewControllerAnimated关闭多个控制器

时间:2016-06-15 11:31:05

标签: ios swift uiviewcontroller uinavigationcontroller

这是我的项目配置:

UINavigationController1 - > UIViewController1 - > UINavigationController2 - > UIViewController2 - > UIViewController3 - > UIViewController4

UIViewController4我这样做: self.presentViewController(newController, animated: true, completion: nil)

当我在这个控制器中完成工作时,我会这样做: self.dismissViewControllerAnimated(true, completion: nil)

最后一个操作不会仅关闭newController并显示UIViewController4,而是显示UIViewController1

deinit{print("test")}中随机的其他控制器的{p> UINavigationController1被调用。

我只想解雇newController并显示UIViewController4

我无法推送/弹出控制器,因为这是一个调用的libraryController。

我该怎么做?

1 个答案:

答案 0 :(得分:0)

用户下面的代码来消除特定视图

 dispatch_async(dispatch_get_main_queue(),
                           {
                            if let topController = UIApplication.topViewController()
                            {
                                topController.dismissViewControllerAnimated(false, completion: nil)
                            }
            })

用于检查超级视图控制器使用下面的代码

if let topController = UIApplication.topViewController() {
                        print("From Super View \(topController)")
                    }

并创建一个扩展

extension UIApplication {

    class func topViewController(viewController: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
        if let nav = viewController as? UINavigationController {
            return topViewController(nav.visibleViewController)
        }
        if let tab = viewController as? UITabBarController {
            if let selected = tab.selectedViewController {
                return topViewController(selected)
            }
        }
        if let presented = viewController?.presentedViewController {
            return topViewController(presented)
        }

        if let slide = viewController as? SlideMenuController {
            return topViewController(slide.currentViewController)
        }
        return viewController
    }
}