0

I have a UITabBarController instansiated in my appDelegate, but I need a way to know when the user presses the different tab bar items (buttons on the tab bar).

-UITabBarDelegate protocol to the rescue (with the required didSelectViewController-method)!

With everything wired up in Interface Builder, how do I obtain a reference to the actual UIViewController subclass instance that corresponds to this tab bar item that was pressed?

I need this reference because I need to call a method in one of my UIViewControllers subclasses every time that tab bar item is pressed.

Any suggestions?

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController
{   
    NSLog(@"%@", [[self.tabBarController selectedViewController] nibName]); // nil, no success here     

    if ([theTabBarController selectedIndex] == 1) {         
        MySecondViewController *reference = (MySecondViewController *) viewController;      

    if ([reference isKindOfClass:[UINavigationController class]]) {
        NSLog(@"OMG. It's a UINavigationController class??!"); // kicks in for some reason, shouldn't reference be a MySecondViewController
    }   
}
maralbjo
  • 963
  • 1
  • 10
  • 16
  • 1
    It appears that you are you using a `UINavigationController` in the tab that is activated. If so, you need to get the top view controller from the `UINavigationController`, and you should get your `MySecondViewController` instance. – pgb Sep 16 '10 at 12:10

1 Answers1

0

Possibly I am not understanding your question correctly, but it seems that what you're asking for is simply the "viewController" parameter which is passed into the method call you've mentioned

- (void)tabBarController:(UITabBarController *)theTabBarController didSelectViewController:(UIViewController *)viewController

UITabBarController also has a property to get the same info

@property(nonatomic, assign) UIViewController *selectedViewController
Johnus
  • 722
  • 6
  • 12