Weston
Weston

Reputation: 1491

UITabBar reload UIView on touch

Is it possible to call a method when a tab bar icon is touched, even if its already the selected icon? I want to make it remove a sub view when touched if the subview is showing.

I'm sure there must be a way to do this because I see it in other apps, but I cannot find any documentation on it.

Upvotes: 0

Views: 1173

Answers (2)

Vincent Guerci
Vincent Guerci

Reputation: 14419

From Apple Documentation for UITabBarDelegate :

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

Sent to the delegate when the user selects a tab bar item. (required)

  • As you noticed I think, and as stated by Apple docs, this message is sent only when a tab is selected

  • An important note, there is already a (not-documented?) default UIKit behavior of tapping a selected tab bar button :

    If the tab contains a UINavigationViewController it will send to it a popToRootViewControllerAnimated: message. You can check this on any iOS application.

So beware before overriding this default (and user expected) behavior, which is, generally, a bad idea. Apple has probably hidden what you want to do in its UIKit API, on purpose.

But if you want anyway does this, here are some ideas:

  • Small, but not easy hack: Once the tabBar has been displayed, recursively browse its .subviews tree to find (I expect, to be confirmed) UIButtons inherited classes (=private UITabBarButtons or something like) to add your target/selector pairs on TouchUp event (you might have to remove default behavior first, which might be tricky)
  • Worst solution, but might be the only one: Do not use UITabBar, but a custom class. I'm pretty sure there are ready-to-use open source components that mimics UITabBar, but sorry, I never used/searched one.

Upvotes: 4

Jay
Jay

Reputation: 864

You can have delegates for the UITabbarcontroller,

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController __OSX_AVAILABLE_STARTING(__MAC_NA,__IPHONE_3_0);
- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController;

Also please go through this document: UITabBarController delegate protocol

Upvotes: 0

Related Questions