HardCode
HardCode

Reputation: 99

UITabBarController set action to specific tab

I would like to set an action to specific tab on UITabBarController. How can I do this? See my code below: Update with code

@interface AccountTabViewController : UIViewController <UITabBarControllerDelegate, UITabBarDelegate>
{
    IBOutlet UITabBarController *tabController;
    IBOutlet UITabBar *tabBar;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if(tabBarController.selectedIndex == 0) 
    {
        [self dismissModalViewControllerAnimated:YES];
    }
}

It never get into the method! Please help.

Upvotes: 1

Views: 6595

Answers (2)

chown
chown

Reputation: 52738

See UITabBarDelegate reference and UITabBarControllerDelegate Protocol Reference.
The method you are looking for is

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
}

or one of these:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController {
}

Also, UITabBarController reference.

Upvotes: 7

0x8badf00d
0x8badf00d

Reputation: 6401

Look at UITabBarControllerDelegate method:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
   if (tabBarController.selectedIndex == 0) 
   {
     // First Tab is selected do something
   }
}

Upvotes: 4

Related Questions