Reputation: 3780
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 it? Thank you.
NSArray *viewContrlls=[[self navController] viewControllers];
[viewContrlls lastObject]
something like,
if ([[viewContrlls lastObject] name] isEqualToString @"viewControllerName"){
Upvotes: 12
Views: 18879
Reputation: 39181
Swift version:
static func getClassNameAsString(className: AnyObject) -> String {
return _stdlib_getDemangledTypeName(className).componentsSeparatedByString(".").last!
}
Upvotes: -2
Reputation: 45598
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.
Upvotes: 14
Reputation: 26385
if ([NSStringFromClass([[viewContrlls lastObject] class]) isEqualToString: @"Whatever"]){
}
You could also use -isKindOfClass
if you prefer to compare directly an instance to a specific class.
Upvotes: 4
Reputation: 2019
Use this It may Help's you
NSString *CurrentSelectedCViewController = NSStringFromClass([[((UINavigationController *)viewController1) visibleViewController] class]);
Upvotes: 33