如何将选项卡栏项目连接到操作?
我有一个 UIView,其中有一个 UITabBar,其中有 4 个 UITabBarItem 组件(全部由 IB 创建)。
我希望当有人单击选项卡栏中的项目时调用 IBAction 函数。但我无法通过 IB 将 tabbaritem 连接到我的操作...我控制从“收到的操作”中拖动,但它不允许我将其连接到 tabbaritem。
谢谢 德肖恩
I have a UIView which has a UITabBar with 4 UITabBarItem components in it (all created from IB).
I want my IBAction function called when someone clicks on the items in the tab bar. But I am unable to connect the tabbaritem to my action via IB... I control drag from the "received actions" but it does not allow me to connect that to the tabbaritem.
Thanks
Deshawn
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
控制将标签栏拖到 IB 中的“文件所有者”并将其设置为委托。接下来将此代码放入您的 viewcontroller.m 文件中:
返回 IB 并使用标签设置选项卡栏中的每个选项卡栏项目。您必须测试 didSelectItem 中的每种情况,因此,如果您有多个情况,请设置不同的标签等。无论如何,这就是我所做的。
Control drag your tabbar to your "File's owner" in IB and set it as the delegate. Next drop this code in your viewcontroller.m file:
Go back to IB and set each tab bar item in your tab bar with a tag. You gotta test for each case in your didSelectItem, so if you got more than one set a different tag etc. Thats how I did it anyway.
从您的问题中不清楚您是否还定义了 UITabBarController。
如果您没有(正如我假设的那样,否则,如果您在 IB 中正确定义了内容,则单击选项卡栏项目应该可以工作),方法是将 UITabBarDelegate 分配给您的 UITabBar 并定义
tabBar:didSelectItem:
看看 UITabBarDelegate 参考
It is not clear from your question if you also defined a UITabBarController.
If you did not (as I assume, otherwise clicking on a tab bar item should work if you correctly defined things in IB), the way to go is assigning a UITabBarDelegate to your UITabBar and define
tabBar:didSelectItem:
Have a look at the reference for UITabBarDelegate
您无法为标签栏项目设置目标操作。他们应该永远有新的观点。唯一的方法是 - 您应该使用委托来确定您的选项卡栏项目被触摸,然后处理在该委托中按下哪个选项卡。
您应该从 Xcode 的属性中为选项卡栏项目设置一个标签值。
You can't set a target action to your tab bar items. They should always have a new view. The only way is - you should use delegates which determine that your tabbar item is touched and then handle which tab is pressed in that delegate.
You should set a tag value to your tab bar item from the properties in xcode.