ios 你如何在 Swift 中检查当前的视图控制器类?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27715861/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 04:10:26  来源:igfitidea点击:

How do you check current view controller class in Swift?

iosswift

提问by user83039

As far as I know, this would work in Objective-C:

据我所知,这适用于 Objective-C:

self.window.rootViewController.class == myViewController

How can I check if the current view controller is a specific one?

如何检查当前视图控制器是否是特定的?

回答by mc01

To check the class in Swift, use "is" (as explained under "checking Type" in the chapter called Type Casting in the Swift Programming Guide)

要在 Swift 中检查类,请使用“is”(如 Swift 编程指南中的“类型转换”一章中的“检查类型”中所述)

if self.window.rootViewController is MyViewController {
    //do something if it's an instance of that class
}

回答by Nhut Duong

Updated for swift3 compiler throwing a fit around ! and ?

针对 swift3 编译器进行了更新!和 ?

if let wd = UIApplication.shared.delegate?.window {
        var vc = wd!.rootViewController
        if(vc is UINavigationController){
            vc = (vc as! UINavigationController).visibleViewController

        }

        if(vc is LogInViewController){
            //your code
        }
    }

回答by Kiran Thapa

You can easily iterate over your view controllers if you are using a navigation controller. And then you can check for the particular instance as:

如果您使用导航控制器,您可以轻松地遍历您的视图控制器。然后您可以检查特定实例为:

if let viewControllers = navigationController?.viewControllers {
    for viewController in viewControllers {
        // some process
        if viewController.isKindOfClass(MenuViewController) {
            println("yes it is")
        }
    } 
}

回答by Ivan Reyes

I had to find the current viewController in AppDelegate. I used this

我必须在 AppDelegate 中找到当前的 viewController。我用过这个

//at top of class
var window:UIWindow?

// inside my method/function
if let viewControllers = window?.rootViewController?.childViewControllers {
    for viewController in viewControllers {
        if viewController.isKindOfClass(MyViewControllerClass) {
            println("Found it!!!")
            }
        }
    }

回答by Christos Chadjikyriacou

Try this

尝试这个

if self is MyViewController {        

}

回答by Trev14

Swift 3

斯威夫特 3

Not sure about you guys, but I'm having a hard time with this one. I did something like this:

不知道你们是怎么想的,但我很难对付这个。我做了这样的事情:

if let window = UIApplication.shared.delegate?.window {
    if var viewController = window?.rootViewController {
        // handle navigation controllers
        if(viewController is UINavigationController){
            viewController = (viewController as! UINavigationController).visibleViewController!
        }
        print(viewController)
    }
}

I kept getting the initial view controller of my app. For some reason it wanted to stay the root view controller no matter what. So I just made a global string type variable currentViewControllerand set its value myself in each viewDidLoad(). All I needed was to tell which screen I was on & this works perfectly for me.

我一直在获取应用程序的初始视图控制器。出于某种原因,它无论如何都想保留根视图控制器。所以我只是创建了一个全局字符串类型变量,currentViewController并在每个viewDidLoad(). 我所需要的只是告诉我我在哪个屏幕上,这对我来说非常适合。

回答by KorinW

To go off of Thapa's answer, you need to cast to the viewcontroller class before using...

要离开 Thapa 的答案,您需要在使用之前转换为 viewcontroller 类...

   if let wd = self.view.window {
        var vc = wd.rootViewController!
        if(vc is UINavigationController){
            vc = (vc as! UINavigationController).visibleViewController
        }
        if(vc is customViewController){
            var viewController : customViewController = vc as! customViewController
   if let wd = self.view.window {
        var vc = wd.rootViewController!
        if(vc is UINavigationController){
            vc = (vc as! UINavigationController).visibleViewController
        }
        if(vc is customViewController){
            var viewController : customViewController = vc as! customViewController

回答by Dary

Swift 4, Swift 5

斯威夫特 4、斯威夫特 5

let viewController = UIApplication.shared.keyWindow?.rootViewController
if viewController is MyViewController {

}

回答by ΩlostA

Check that way that worked better for me What is .self

检查对我更有效的方式什么是 .self

if ((self.window.rootViewController?.isKind(of: WebViewController.self))!)
{
  //code
}

回答by Owais Munawar

For types you can use isand if it is your own viewcontroller class then you need to use isKindOfClasslike:

对于您可以使用的类型is,如果它是您自己的 viewcontroller 类,那么您需要使用isKindOfClass

let vcOnTop = self.embeddedNav.viewControllers[self.embeddedNav.viewControllers.count-1]
            if vcOnTop.isKindOfClass(VcShowDirections){
                return
            }