我有这段代码 - (void)tabBar:(UITabBar*)tabBar didSelectItem:(UITabBarItem*)item
我正在寻找的是一个代码片段,说明如何检测代理中按下哪个按钮大概是使用项目.
所以也许我在标签栏上排列了1到4个按钮.我的用户按下按钮位置2.我需要知道,所以我可以调出适合该按钮的视图.
我尝试过类似的东西,但它没有用.
NSInteger *barIndex = [[barTab items] IndexofObject:item];
Run Code Online (Sandbox Code Playgroud)
如果有人可以提供一些非常好的工作示例代码.
提前致谢.
当你创建自己UITabBarItem的时,你会想要给每个人一个特定的标签.当你的- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item方法被调用时,读取标签数出来.
NSInteger tag = item.tag;
Run Code Online (Sandbox Code Playgroud)
使用项目的索引不适用于标签栏,因为用户可以更改选项卡的顺序.
好的做法是为每个标记使用枚举,这样就不会在代码中散布一堆"随机"数字.
typdef enum {
JPButton1,
JPButton2,
JPButton3
} JPButtonType
Run Code Online (Sandbox Code Playgroud)
然后在你的tabBar:didSelectItem:方法中你可以像这样测试标签:
if (item.tag == JPButton1) {
// do some stuff with button one here
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7645 次 |
| 最近记录: |