简体   繁体   English

如何从AppDelegate获取当前可见的viewController

[英]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.

相关问题 从Appdelegate获取ViewController - Get ViewController from Appdelegate 如何从appdelegate获取ViewController中的NSMutableArray的值 - How to get the value of NSMutableArray in viewcontroller from the appdelegate 如何在非viewcontroller类中获取可见/当前viewcontroller? 迅速 - How get visible / current viewcontroller in non-viewcontroller class? Swift 如何从Appdelegate打开Viewcontroller? - How to open Viewcontroller from Appdelegate? 如果它位于TabBarController中,如何从AppDelegate访问ViewController的属性? - How to get access to ViewController's property from AppDelegate if it is in TabBarController? 除了从appDelegate获取它之外,如何获取viewController的managedObjectContext? - How to get managedObjectContext for viewController other than getting it from appDelegate? 如何在AppDelegate上从TabBarController获取ViewController? - How can I get ViewController from TabBarController on AppDelegate? 从appdelegate获取当前可见的modalViewController - Getting current visible modalViewController from appdelegate 在iOS中将数据从AppDelegate获取到ViewController - Get data from AppDelegate to ViewController in iOS 从 AppDelegate 中的 ViewController 获取参数。 - Get param from ViewController in AppDelegate.
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM