Reputation: 61
I would like to know if the rootViewController in a navigation controller stack is of specific class. How to do such a thing ?
Thx for helping,
Stephane
Upvotes: 3
Views: 3785
Reputation: 13045
How to check your current rootViewController
and use it in an if statement
:
// Get your current rootViewController
NSLog(@"My current rootViewController is: %@", [[[UIApplication sharedApplication].delegate.window.rootViewController class]);
// Use in an if statement
UIViewController *rootViewController = [UIApplication sharedApplication].delegate.window.rootViewController;
if ([rootViewController isKindOfClass:[MyViewController class]])
{
NSLog(@"Your rootViewController is MyViewController!!");
}
Upvotes: 0
Reputation: 51374
The navigation controllers root view controller is,
id rootVC = [[navigationController viewControllers] objectAtIndex:0];
And check the class of rootVC like this,
if ([rootVC isKindOfClass:[YourClass class]]) {
Upvotes: 2
Reputation: 71
Perhaps you can use the method isKindOfClass
[rootViewController isKindOfClass: [RootViewController class]];
Upvotes: 0
Reputation: 10422
You can check the class with:
if ([rootViewController isKindOfClass:[YourClass class]]){
}else if ([rootViewController isKindOfClass:[AnotherClass class]]){
}else{
}
Upvotes: 0
Reputation: 28750
Here you go buddy
id rootController = [self.navigationController rootViewController];
if([rootController isKindOfClass:[YourDesiredController class]]){
//do something
}
Upvotes: 11