如何将选项卡栏项目连接到操作?

发布于 2024-11-27 08:32:25 字数 196 浏览 5 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

日久见人心 2024-12-04 08:32:25

控制将标签栏拖到 IB 中的“文件所有者”并将其设置为委托。接下来将此代码放入您的 viewcontroller.m 文件中:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if(item.tag == 0)
    {
        //party like its 1999 right here
    }
}

返回 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:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if(item.tag == 0)
    {
        //party like its 1999 right here
    }
}

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.

淑女气质 2024-12-04 08:32:25

从您的问题中不清楚您是否还定义了 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

甲如呢乙后呢 2024-12-04 08:32:25

您无法为标签栏项目设置目标操作。他们应该永远有新的观点。唯一的方法是 - 您应该使用委托来确定您的选项卡栏项目被触摸,然后处理在该委托中按下哪个选项卡。

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if(item.tag == 1)
    {
    }
}

您应该从 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.

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item
{
    if(item.tag == 1)
    {
    }
}

You should set a tag value to your tab bar item from the properties in xcode.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文