Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

set up an action with a tab bar item - iphone

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

like image 733
coder Avatar asked Sep 27 '11 20:09

coder


People also ask

How do I put the top tab bar in iOS?

Open the Settings app. Scroll down and tap Safari. Scroll down to the Tabs section and locate the option to switch between the Tab Bar and a Single Tab. Select Single Tab to move the Address Bar back to the top of the screen, or select Tab Bar to move it back to the bottom if you change your mind later on.

What is tab bar on iPhone?

In the Safari section of the Settings app, you can choose your tab view. The "Tab Bar" option moves the address bar to the bottom of the Safari interface, which is the new design. There is a dedicated control bar at the bottom of the Safari interface, and above that, an integrated tab bar and address bar.

How do I add items to my tab bar controller?

To add a tab, first drag a new View Controller object to the storybard. Next control-drag from the tab bar controller to new view controller and select view controllers under Relationship Segue . Your tab bar controller will update with a new tab.


1 Answers

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

like image 162
Matt Williamson Avatar answered Nov 15 '22 09:11

Matt Williamson