[英]How to get the current visible viewController from AppDelegate
So, I'm using the method bellow from UIApplication
extension to get the top view controller: 所以,我正在使用
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)
}
return controller
}
But the problem is: It always returns UIViewController
. 但问题是:它总是返回
UIViewController
。 But I need to check if it is MyViewController
for example. 但我需要检查它是否是
MyViewController
。 How do I achieve that? 我如何实现这一目标?
To use the UIViewController
as MyViewController
: 要将
UIViewController
用作MyViewController
:
if let myViewController = UIApplication.topViewController() as? MyViewController { ... }
or if you just want to check that the UIViewController
is of type MyViewController
: 或者如果您只是想检查
UIViewController
是否为MyViewController
类型:
if UIApplication.topViewController() is MyViewController { ... }
You can do a conditional check with an if-let
statement like this: 您可以使用
if-let
语句进行条件检查,如下所示:
if let presented = controller?.presentedViewController as? MyViewController {
// it is a MyViewController
}
You can also just directly check if the UIViewController is that type of class like this: 您也可以直接检查UIViewController是否是这种类型的类:
if controller?.presentedViewController is MyViewController {
// it is a MyViewController
}
Try this: 尝试这个:
if let presented = controller?.presentedViewController as? MyViewController {
...
Do conditional casting on the return value to safely check its type. 对返回值进行条件转换以安全地检查其类型。
if let currentVC = UIApplication.topViewController() as? MyViewController {
//the type of currentVC is MyViewController inside the if statement, use it as you want to
}
Your whole function implementation is flawed, if it actually worked, it would lead to infinite recursion. 你的整个函数实现是有缺陷的,如果它实际工作,它将导致无限递归。 Once you find out the type of your current top view controller in your if statements, you are calling the same function again with the current root controller as its input value.
一旦在if语句中找到当前顶视图控制器的类型,就会再次使用当前根控制器作为其输入值调用相同的函数。 Your function only ever exists, if it reaches either a call from a view controller, whose class is none of the ones specified in your optional bindings.
只有来自视图控制器的调用时,您的函数才会存在,该类的类不是可选绑定中指定的类。
Moreover, your whole implementation doesn't do anything at the moment. 而且,你的整个实现目前都没有做任何事情。 You find out the type of your root view controller, but than you upcast it by returning a value of type
UIViewController
. 您可以找到根视图控制器的类型,但是通过返回
UIViewController
类型的值来转发它。
You can check it in following ways 您可以通过以下方式进行检查
class func topViewController(controller: UIViewController? = UIApplication.shared.keyWindow?.rootViewController) -> UIViewController? {
if let navigationController = controller as? UINavigationController {
return topViewController(controller: navigationController.visibleViewController)
}
else if let tabController = controller as? UITabBarController {
if let selected = tabController.selectedViewController {
return topViewController(controller: selected)
}
}
else if let presented = controller?.presentedViewController {
return topViewController(controller: presented)
}
return controller
}
// Answer
if let topVC = AppDelegate.topViewController() as? MyViewController {
// Here your topVC is MyViewController
}
// or
if let topVC = AppDelegate.topViewController() {
if topVC is MyViewController {
// Here your topVC is MyViewController
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.