【问题标题】:Detecting which view is present in applicationWillEnterForeground检测 applicationWillEnterForeground 中存在哪个视图
【发布时间】:2018-04-24 10:37:27
【问题描述】:

我想知道当应用程序进入前台时存在哪个视图。

这怎么可能?

func applicationWillEnterForeground(_ application: UIApplication) {

            if (storyboardID == "myview") {

               //do sth

            }

        }

【问题讨论】:

标签: ios swift


【解决方案1】:
func getCurrentViewController() -> UIViewController? {

      if let rootController = UIApplication.shared.keyWindow?.rootViewController {
      var currentController: UIViewController! = rootController
         while (currentController.presentedViewController != nil) {
                currentController = currentController.presentedViewController
            }
            return currentController
        }
        return nil
    }

使用上面的代码你会得到当前的View Controller。

使用示例

func applicationWillEnterForeground(_ application: UIApplication) {
    if let storyboardID = getCurrentViewController()?.restorationIdentifier, 
           storyboardID == "myview") {
           //do sth
        }
    }

【讨论】:

    【解决方案2】:
    public extension UIWindow {
        public var visibleViewController: UIViewController? {
            return UIWindow.getVisibleViewControllerFrom(self.rootViewController)
        }
    
        public static func getVisibleViewControllerFrom(_ vc: UIViewController?) -> UIViewController? {
            if let nc = vc as? UINavigationController {
                return UIWindow.getVisibleViewControllerFrom(nc.visibleViewController)
            } else if let tc = vc as? UITabBarController {
                return UIWindow.getVisibleViewControllerFrom(tc.selectedViewController)
            } else {
                if let pvc = vc?.presentedViewController {
                    return UIWindow.getVisibleViewControllerFrom(pvc)
                } else {
                    return vc
                }
            }
        }
    }
    

    Swift 中 UIApplication 的简单扩展(甚至关心 UITabBarController 中的 moreNavigationController:

    extension UIApplication {
        class func topViewController(base: UIViewController? = UIApplication.sharedApplication().keyWindow?.rootViewController) -> UIViewController? {
    
            if let nav = base as? UINavigationController {
                return topViewController(base: nav.visibleViewController)
            }
    
            if let tab = base as? UITabBarController {
                let moreNavigationController = tab.moreNavigationController
    
                if let top = moreNavigationController.topViewController where top.view.window != nil {
                    return topViewController(top)
                } else if let selected = tab.selectedViewController {
                    return topViewController(selected)
                }
            }
    
            if let presented = base?.presentedViewController {
                return topViewController(base: presented)
            }
    
            return base
        }
    }
    

    简单用法:

       if let rootViewController = UIApplication.topViewController() {
            //do sth with root view controller
        }
    

    【讨论】:

    • 我想通过 viewcontroller 的故事板 ID 来检查它。这可能吗?
    • @op 使用 stackoverflow.com/questions/13708660/… ;) 然后用它来比较。这不会改变这个答案其余部分的 100%,所以 +1
    猜你喜欢
    • 2015-11-09
    • 2011-08-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多