Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selected index on the "more" view of a UITabbar

How can I manage the user selection on the "more" view of a UITabBar? I have this code to manage the UITabBarItems selections:

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {    
    if (!(viewController == tabBarController.moreNavigationController)) {
        int index = tabBarController.selectedIndex;
        [[DataManager sharedInstance] setCurrentTabbarIndex:index];
    }
}

It works fine for the visible UITabBarItems, but when the user select some item from the "more" view I never get informed about that. Is there some way to catch the user item selection of the "more" view? Thanks!

like image 275
Sebastian Avatar asked Mar 20 '11 00:03

Sebastian


2 Answers

The "more" view of a UITabBarController is handled separately from the other views. Apple's discussion on this subject says the following:

[The 'moreNavigationController'] property always contains a valid More navigation controller, even if a More button is not displayed on the screen. You can use the value of this property to select the More navigation controller in the tab bar interface or to compare it against the currently selected view controller.

Do not add the object stored in this property to your tab bar interface manually. The More controller is displayed automatically by the tab bar controller as it is needed. You must also not look for the More navigation controller in the array of view controllers stored in the viewControllers property. The tab bar controller does not include the More navigation controller in that array of objects.

Judging from that I would think that you can do something like:

int index = tabBarController.selectedIndex;
if (tabBarController.selectedViewController == 
    tabBarController.moreNavigationController) {
    index = -1;  //assign some placeholder index for the "More" controller
}
like image 152
aroth Avatar answered Oct 03 '22 06:10

aroth


A very late answer for a very old question, but here it is anyway, in case someone stumbles upon this.

The solution is to assign yourself as the delegate of the "more" navigation controller. You already have a class that adopts the UITabBarControllerDelegate protocol, so the .h file might look like this:

@interface MyDelegate : NSObject <UITabBarControllerDelegate, UINavigationControllerDelegate>
{
}

Wherever you are assigning your class as delegate, do this:

- (void) assignDelegate:(MyDelegate)myDelegate toTabBarController:(UITabBarController*)tabBarController
{
  tabBarController.delegate = myDelegate;
  tabBarController.moreNavigationController.delegate = myDelegate;
}

And finally, in your delegate class add this method:

- (void) navigationController:(UINavigationController*)navigationController didShowViewController:(UIViewController*)viewController animated:(BOOL)animated
{
     // add code to handle the event
}

Note that none of your delegate methods are invoked when you change tabs programmatically.

like image 34
herzbube Avatar answered Oct 03 '22 06:10

herzbube