How do you check current view controller class in Swift?


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

self.window.rootViewController.class == myViewController

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

This question is tagged with ios swift

~ Asked on 2014-12-31 03:45:46

The Best Answer is


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

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

~ Answered on 2014-12-31 03:55:02


Updated for swift3 compiler throwing a fit around ! and ?

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

~ Answered on 2014-12-31 04:44:14

Most Viewed Questions: