user963160
user963160

Reputation: 61

How to check if rootViewController is of a specific class?

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

Answers (5)

emotality
emotality

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

EmptyStack
EmptyStack

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

bjornjohansson1
bjornjohansson1

Reputation: 71

Perhaps you can use the method isKindOfClass

[rootViewController isKindOfClass: [RootViewController class]];

Upvotes: 0

Matteo Alessani
Matteo Alessani

Reputation: 10422

You can check the class with:

if ([rootViewController isKindOfClass:[YourClass class]]){

}else if ([rootViewController isKindOfClass:[AnotherClass class]]){

}else{
}

Upvotes: 0

Rahul Vyas
Rahul Vyas

Reputation: 28750

Here you go buddy

id rootController = [self.navigationController rootViewController];
if([rootController isKindOfClass:[YourDesiredController class]]){
  //do something
 }

Upvotes: 11

Related Questions