views:

94

answers:

2

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
    }   
}
A: 

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
A: 

Hi Maralbjo,

I am also facing the same problem you have mentioned above.

Have you got the solution for this. In my application I want to call a reload data in tableview whenever the tabbar item is clicked. Please explain.

Thanks in advance. Sudheer

Place your code in the method - (void) viewWillAppear:(BOOL)animated
maralbjo