iPhone:无法从 uitabbaritem 读取徽章值

发布于 2024-08-23 07:20:03 字数 372 浏览 5 评论 0原文

我试图以编程方式确定我的应用程序中的特定选项卡栏项目是否有徽章。

当我在调试时,从视觉上看,我可以清楚地看到它确实如此。但是当我在有问题的 viewController 中运行此代码时:

UITabBarItem* thisVCsTabBarItem = self.tabBarItem;
NSString* badgeValue = thisVCsTabBarItem.badgeValue;

...badgeValue 为零。当我在调试器中检查 thisVCsTabBarItem 时,它的 _badgeValue 成员为零。

这是怎么回事?我应该做一些不同的事情来尝试从选项卡栏项目中读取这个值吗?

谢谢。

I'm trying to figure out programmatically if a particular tab bar item in my app has a badge.

While I'm debugging, visually, I can plainly see that it does. But when I run this code in the viewController in question:

UITabBarItem* thisVCsTabBarItem = self.tabBarItem;
NSString* badgeValue = thisVCsTabBarItem.badgeValue;

...badgeValue is nil. And when I inspect thisVCsTabBarItem in the debugger, its _badgeValue member is nil.

What's going on here? Should I be doing something differently in trying to read this value from the tab bar item?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

静若繁花 2024-08-30 07:20:03

查看一些使用 UITabBarItem badgeValue 属性的代码,我发现 self.tabBarItem.badgeValue 返回 nil而 self.navigationController.tabBarItem.badgeValue 返回正确的值。会是这样吗?

问题是,自动完成实际上在 self 之后给了我 tabBarItem。因此很容易犯错误。

Looking at some code where I use the UITabBarItem badgeValue property, I see that self.tabBarItem.badgeValue returns nil while self.navigationController.tabBarItem.badgeValue returns the correct value. Could that be it?

The thing is that the auto-completion actually gives me tabBarItem after self. Easy to make a mistake because of that.

滿滿的愛 2024-08-30 07:20:03

我对“下载”选项卡执行类似的操作:

for (UITabBarItem* item in self.tabBarController.tabBar.items) {
    if (item.tag == 3) {
        if (downloadCount > 0) {
            item.badgeValue = [NSString stringWithFormat: @"%d", downloadCount];
        } else {
            item.badgeValue = nil;
        }
    }
}

我认为您不应该直接访问 tabBarItem。最好在 tabBarController 的 items 数组中找到您的项目。

I do something like this for a Downloads tab:

for (UITabBarItem* item in self.tabBarController.tabBar.items) {
    if (item.tag == 3) {
        if (downloadCount > 0) {
            item.badgeValue = [NSString stringWithFormat: @"%d", downloadCount];
        } else {
            item.badgeValue = nil;
        }
    }
}

I don't think you are supposed to access the tabBarItem directly. It is better find your item in the tabBarController's items array.

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