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
    }   
}
1
  • 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, 2010 at 12:10

1 Answer 1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.