shivam
shivam

Reputation: 1140

Check the type of rootviewcontroller

I have many navigation controllers and one tab bar controller in my app. Now i want to check if currently i am on navigation view or tab bar view. How can i check for my current view class.

NSString *className=self.window.rootViewController.description;
NSLog(@"class name is %@ ",className);

When I am on navigation controller view the output is :

class name is <UINavigationController: 0x1cd78780>

And when i am on tab bar controller, It prints

class name is <UITabBarController: 0x1cdbd8d0>

How can i recognize them. Thanks in advance.

Upvotes: 0

Views: 1280

Answers (2)

Sohaib
Sohaib

Reputation: 11297

Its easy. You already spotted if its UINavigationController now all you need is to get the top view controller from your navigation controller

NSString *className = navigationController.topViewController.description;
NSLog(@"class name is %@ ",className);

Upvotes: 1

AntonPalich
AntonPalich

Reputation: 1498

Class rootClass = [self.window.rootViewController class];

if (rootClass == [UINavigationController class]) {

} else if (rootClass == [UITabBarController class]) {

}

Upvotes: 3

Related Questions