Customize action using a tab element - iphone

I don’t know if this is possible, but is this a way to handle a tab bar item like a normal button click (action)? Here's why: I have a tab bar in my application, and for most of the tab bar items I don't change the screen by touch, but I change the data displayed in the view. It seems that it will be a bit overboard to use TabBarAppDelegate for this ... is there a better way?

thanks

+4
source share
3 answers

The easiest way is UITabBarDelegate. I'm sorry. Add your class and inherit the protocol by adding <UITabBarDelegate> after defining your class, for example:

 @interface MyViewController : UIViewController<UITabBarDelegate> 

and then define the tabBar:didSelectItem: method in this class, for example.

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

Then set the delegate in your tab as follows: myTabBar.delegate = myClassInstance . The tabBar:didSelectItem: method can be anywhere, including your view controller, and here you get the event the button was clicked on. More details here:

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

+14
source

Personally, I used the UISegmentedControl and put it in a UIToolbar or UINavigationBar . Thus, you get the same touch effect on data changes, but you should not switch to view controllers (or try to bypass the switching tabs).

+3
source

I suggest going with UIToolbar. Or create your own UIView and create it to your liking.

+1
source

All Articles