5

I don't no if this is possible, but this there a way to treat a tab bar item click like a regular button click(action)? Here's the reason: I have a tab bar in my application, and for most of the tab bar items, I don't change the screen upon a touch, but I do change the data displayed in the view. It seems that it would be going a bit overboard to use a TabBarAppDelegate for this...is there a better way?

Thanks

coder
  • 10,460
  • 17
  • 72
  • 125

3 Answers3

14

The most straightforward way is the UITabBarDelegate. Sorry. Implement your class and inherit the protocol by adding <UITabBarDelegate> after your class definition e.g.:

@interface MyViewController : UIViewController<UITabBarDelegate>

and then define the method tabBar:didSelectItem: in that class e.g.

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    // Do Stuff!
    // if(item.title == @"First") {...}
}

Then set the delegate on your tabbar like so: myTabBar.delegate = myClassInstance. The tabBar:didSelectItem: method can be anywhere including your view controller and is where you will get the event that the button was clicked. More info here:

http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITabBarDelegate_Protocol/Reference/Reference.html

Matt Williamson
  • 39,165
  • 10
  • 64
  • 72
  • 1
    Got it. Thanks! Also this is a good reference I found: https://discussions.apple.com/thread/2099944?start=0&tstart=0 – coder Sep 28 '11 at 13:12
3

Personally, I would use UISegmentedControl and place it in a UIToolbar or UINavigationBar. That way you get the same touch-to-change-data effect, but don't have to swap view controllers (or try to bypass switching tabs)

justin
  • 5,811
  • 3
  • 29
  • 32
1

I'd suggest going with a UIToolbar. Or make your own custom UIView and design it to your liking

Joey
  • 525
  • 1
  • 6
  • 16