kygcoleman
kygcoleman

Reputation: 744

Get currentViewController of View Controller in UITabBarController in UINavigation Controller

I would like to know how I can find the current view controller if I have a UITabBarController and within that TabBarController I have 4 different UINavigationControllers and then that NavigationController obviously has the stack of view controllers. In my app delegate I am trying to find out this information so that I can stop an in-app push notification from appearing. So I am looking to do something like:

if (self.currentViewController != self.chatViewController) {
   //Show the notification everywhere else except here(here being self.chatViewController)
}

Upvotes: 0

Views: 856

Answers (1)

Jasmeet Singh
Jasmeet Singh

Reputation: 564

Try doing this:

    UITabBarController *tabBarControler = (UITabBarController*)self.window.rootViewController;
    UINavigationController *navController = [[tabBarControler viewControllers] objectAtIndex:<selected_index>];
    NSArray *navViewControllers = [navController viewControllers];

You can check for the present viewController like this.

if (navController.topViewController != self.chatViewController) {
 //Show the notification every else except here(here being self.chatViewController)
}

Edit by @kylecman

What I ended up doing was using the tabbarcontroller and navigation controller instances and not using the array(have not found the need for that)

and then I prepared an IF statement for the viewController's class

if (![navController.topViewController isKindOfClass:[ChatView class]] && ![navController.topViewController isKindOfClass:[MessagesViewController class]]) {

Upvotes: 3

Related Questions