Get UIViewController class name in iOS

后端 未结 4 2256
广开言路
广开言路 2021-02-20 01:58

I am getting viewControllers on navigationController stack as follows. Now I need to check if controller on top is one of known vc. How to get vc class name in order to compare

相关标签:
4条回答
  • 2021-02-20 02:22

    Use this It may Help's you

     NSString *CurrentSelectedCViewController = NSStringFromClass([[((UINavigationController *)viewController1) visibleViewController] class]);
    
    0 讨论(0)
  • 2021-02-20 02:29

    The most common technique is to use -isKindOfClass:

    if ([[viewContrells lastObject] isKindOfClass:MyViewController.class]]) {
        // ...
    }
    

    Using NSStringFromClass to compare strings is not a very nice solution because your code will break if you refactor the view controller to rename it.

    0 讨论(0)
  • 2021-02-20 02:34
    if ([NSStringFromClass([[viewContrlls lastObject] class]) isEqualToString: @"Whatever"]){
    }
    

    You could also use -isKindOfClass if you prefer to compare directly an instance to a specific class.

    0 讨论(0)
  • 2021-02-20 02:38

    Swift version:

    static func getClassNameAsString(className: AnyObject) -> String {
        return _stdlib_getDemangledTypeName(className).componentsSeparatedByString(".").last!
    }
    
    0 讨论(0)
提交回复
热议问题