[英]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?
看来,使用TabBarAppDelegate会有点过分......有更好的方法吗?
Thanks 谢谢
The most straightforward way is the UITabBarDelegate. 最简单的方法是UITabBarDelegate。 Sorry.
抱歉。 Implement your class and inherit the protocol by adding
<UITabBarDelegate>
after your class definition eg: 通过在类定义后添加
<UITabBarDelegate>
实现您的类并继承协议,例如:
@interface MyViewController : UIViewController<UITabBarDelegate>
and then define the method tabBar:didSelectItem:
in that class eg 然后定义方法
tabBar:didSelectItem:
在该类中
- (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
. 然后在tabbar上设置委托,如下所示:
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. tabBar:didSelectItem:
方法可以在任何地方,包括您的视图控制器,并且您将获得单击该按钮的事件。 More info here: 更多信息:
http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITabBarDelegate_Protocol/Reference/Reference.html http://developer.apple.com/library/ios/#DOCUMENTATION/UIKit/Reference/UITabBarDelegate_Protocol/Reference/Reference.html
Personally, I would use UISegmentedControl
and place it in a UIToolbar
or UINavigationBar
. 就个人而言,我会使用
UISegmentedControl
并将其放在UIToolbar
或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) 这样您就可以获得相同的触摸 - 更改 - 数据效果,但不必交换视图控制器(或尝试绕过切换选项卡)
I'd suggest going with a UIToolbar. 我建议你去UIToolbar。 Or make your own custom UIView and design it to your liking
或者制作自己的自定义UIView并根据自己的喜好进行设计
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.