Jhorra
Jhorra

Reputation: 6321

Determine which tab select in tabbarcontroller

I've implemented the following in my app delegate and attempted to check the selected index. What I've found though is this value is the tab that it's on when a new tab is clicked, not the new tab. Is there a way to find which tab was selected?

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
{
    int *i = tabBarController.selectedIndex;
}

Upvotes: 0

Views: 1676

Answers (3)

superarts.org
superarts.org

Reputation: 7238

Swift version:

func tabBarController(tabBarController: UITabBarController, shouldSelectViewController viewController: UIViewController) -> Bool {
    var shouldSelect = true
    if let viewControllers = tabBarController.viewControllers where viewControllers.indexOf(viewController) == lastIndex {
        shouldSelect = false
    }
    return shouldSelect
}

Upvotes: 0

Martin R
Martin R

Reputation: 539765

If I am not mistaken, you can get the index of the selected tab with

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController
    NSUInteger selectedIndex = [tabBarController.viewControllers indexOfObject:viewController];
    // ...
    return YES or NO;
}

Upvotes: 3

rmaddy
rmaddy

Reputation: 318814

You want to use the tabBarController:didSelectViewController: delegate method. That is called after the tab is selected.

And your int *i should actually be int i or better yet, NSUInteger i.

Upvotes: 0

Related Questions