首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在UIAlertController下的最高级UIAlertController

在UIAlertController下的最高级UIAlertController
EN

Stack Overflow用户
提问于 2016-03-29 12:36:18
回答 6查看 3.7K关注 0票数 5

我使用下面的扩展来查找top most ViewController。如果出现警报,则上面的代码将给出UIAlertController。如何在 UIAlertController下获得顶部视图控制器UIAlertController

EN

回答 6

Stack Overflow用户

发布于 2020-05-21 00:14:01

创建一个如下所示的UIApplication扩展,UIApplication.topViewController()将返回UIAlertController下最顶部的UIViewController

iOS 13+

代码语言:javascript
复制
extension UIApplication {

    class func topViewController(controller: UIViewController? = UIApplication.shared.windows.first?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            return topViewController(controller: navigationController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(controller: selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topViewController(controller: presented)
        }
        if let alert = controller as? UIAlertController {
            if let navigationController = alert.presentingViewController as? UINavigationController {
                return navigationController.viewControllers.last
            }
            return alert.presentingViewController
        }
        return controller
    }

}

iOS 12-

代码语言:javascript
复制
extension UIApplication {

    class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
        if let navigationController = controller as? UINavigationController {
            return topViewController(controller: navigationController.visibleViewController)
        }
        if let tabController = controller as? UITabBarController {
            if let selected = tabController.selectedViewController {
                return topViewController(controller: selected)
            }
        }
        if let presented = controller?.presentedViewController {
            return topViewController(controller: presented)
        }
        if let alert = controller as? UIAlertController {
            if let navigationController = alert.presentingViewController as? UINavigationController {
                return navigationController.viewControllers.last
            }
            return alert.presentingViewController
        }
        return controller
    }

}
票数 4
EN

Stack Overflow用户

发布于 2016-03-29 12:41:40

您可以检查下一个viewController是否为UIAlertController,如果是,则返回它的父级。就像这样:

代码语言:javascript
复制
if let presented = base as? UIAlertController {
  return base.presentingViewController
}

在返回前使用的扩展名中添加此内容。

更新的

代码语言:javascript
复制
extension UIApplication {
   class func topViewController(base: UIViewController? =    (UIApplication.sharedApplication().delegate as! AppDelegate).window?.rootViewController) -> UIViewController? {
      if let nav = base as? UINavigationController {
         return topViewController(base: nav.visibleViewController)
      }
      if let tab = base as? UITabBarController {
         if let selected = tab.selectedViewController {
             return topViewController(base: selected)
         }
      }
      if let presented = base?.presentedViewController {
         return topViewController(base: presented)
      }

      if let alert = base as? UIAlertController {
         return alert.presentingViewController
      }

      return base
   }
}
票数 1
EN

Stack Overflow用户

发布于 2016-03-29 13:11:53

可以使用UIAlertControllerpresentingViewController属性获取它的父控制器。

代码语言:javascript
复制
extension UIApplication {
  class func topViewController(base: UIViewController? = (UIApplication.sharedApplication().delegate as! AppDelegate).window?.rootViewController) -> UIViewController? {
    if let nav = base as? UINavigationController {
      return topViewController(base: nav.visibleViewController)
    }
    if let tab = base as? UITabBarController {
      if let selected = tab.selectedViewController {
        return topViewController(base: selected)
      }
    }
    if let alert = base as? UIAlertController {
      if let presenting = alert.presentingViewController {
        return topViewController(base: presenting)
      }
    }
    if let presented = base?.presentedViewController {
      return topViewController(base: presented)
    }
    return base
  }
}

在代码中使用这些更改,而不是在XCode上进行测试。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36284476

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档