0

I'm trying to switch tabs programmatically and ensure that on switch the respective Navigation Controller pops to the root view controller. Is there a method that is invoked when tab bar is switched manually or programmatically?

Note: -(void)tabBarController:didSelectViewController: is only invoked on manual switching of the tab bars

Nav
  • 1,185
  • 16
  • 23

2 Answers2

1

You could try also calling didSelectViewController programmatically. Check out babbidi's answer here:

How to trigger method "tabBarController:didSelectViewController:" programmatically?

Community
  • 1
  • 1
Derek
  • 132
  • 1
  • 5
0

FWIW, the other way of doing this is using an observer on selectedViewController.

// Add Observer
// Note: tabBarController.selectedIndex is not observed as it does not call observeValueForKeyPath on manual switch
[self.tabBarController addObserver:self forKeyPath:@"selectedViewController" options:NSKeyValueObservingOptionNew context:@"changedTabbarIndex"];

// Method for Handling Observations
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    NSString *action = (__bridge NSString*)context;
    if([action isEqualToString:@"changedTabbarIndex"])
    {
        // Stuff to do on selected Tab changed
    }
}

// Change selectedViewController
[self.tabBarController setSelectedViewController:[[self.tabBarController viewControllers] objectAtIndex:kSomeTab]];

More Info: I didn't get any notifications when I touched on tabbar items

Community
  • 1
  • 1
Nav
  • 1,185
  • 16
  • 23