How do you check current view controller class in Swift?

前端 未结 15 782
梦谈多话
梦谈多话 2020-12-13 01:50

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

self.window.rootViewController.class == myViewController

How can I check if the current v

15条回答
  •  感情败类
    2020-12-13 02:19

    Swift 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 currentViewController and set its value myself in each viewDidLoad(). All I needed was to tell which screen I was on & this works perfectly for me.

提交回复
热议问题