为尚未分配的viewController设置tabBarItem.badgeValue?
我在 Xcode 的新项目中使用了选项卡栏应用程序。在 IB 中,我添加了 NavigationController,总共有五个。在应用程序启动用户登录时,我想为第五个 tabBarItem 设置徽章值,但我几乎在任何地方都无法这样做。
我更愿意在登录代码运行后执行此操作,但我不确定如何告诉第五个 viewController 获取徽章值。另外,我在 viewController 中尝试过,但 self.tabBarItem.badgeValue 也不起作用。
I have used the Tab Bar Application in New Project in Xcode. In IB, I have added NavigationControllers and I have five in total. On app launch the user login, and I would like to set a badgeValue for the fifth tabBarItem, but I am unable to do so, pretty much anywhere.
I'd prefer to do it after the login code has run, but I am not sure how I tell the fifth viewController to get the badgeValue. Also, I have tried it inside the viewController but self.tabBarItem.badgeValue doesn't work either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
必须创建视图控制器才能使其工作。我能想到的唯一一件事是您必须访问导航控制器的选项卡栏项目,而不是导航控制器的根控制器。因此,从导航控制器的根控制器类来看,这应该可以工作:
或者,从 applicationDidFinishLaunching 来看:
这些都不起作用吗?
The view controller must have been created for this to work. The only other thing I can think of is that you have to access the tab bar item of your navigation controller and not of the navigation controller's root controller. So from the nav controller's root controller class, this should work:
Or, from applicationDidFinishLaunching:
Does any of this not work?
如果您为 tabBarItem 设置了标签 (
[UITabBarItem initWithTitle:image:tag:]
) 并且您不想创建 IBOutlet 来查找正确的 badgeValue,则可以使用此标签来确保设置正确的值:If you set a tag for the tabBarItem (
[UITabBarItem initWithTitle:image:tag:]
) and you don't want to create an IBOutlet for finding the right badgeValue, you can use this tag to ensure setting the right value:这不是一个完美的解决方案,但我发现这是可行的:
SelectedViewController 默认为 0,并且除了 selectedViewController 之外,我无法为任何内容设置徽章值。因此,如果我将 selectedViewController 设置为所需的 on,然后设置它的 badgeValue,然后将 selectedViewController 设置回 0,它不会扰乱应用程序的启动,一切都会正常工作。
Not a perfect solution, but I found this to work:
SelectedViewController is 0 by default, and I could not set the badgeValue for anything but the selectedViewController. Therefore, if I set the selectedViewController to the desired on, then set it's badgeValue and then set the selectedViewController back to 0, it will not mess up the app's launch and everything will work fine.
使用此方法设置 badgedValue 的问题
是:如果您在拥有超过 5 个选项卡项目时使用“更多”功能。如果用户将相关选项卡项移动到其他位置,您将把徽章值设置为不同的选项卡项。
如果您已经开始使用 Apple 的导航/选项卡控制器模板,只需在应用程序委托中创建一个 IBOutlet,链接到您想要更新的特定选项卡项。
然后使用以下方法从您喜欢的任何地方从应用程序委托访问您的选项卡项:
The issue with setting the badgeValue with this approach:
is if you are using the More feature when you have more than 5 tab items. If the user moves the tab item in question to a different position you'll be setting the badge value to a different tab item.
If you have started with Apple's navigation/tab controller template, simply create an IBOutlet in the Application Delegate linking to the specific tab item you wish to update.
Then access your tab item from application delegate from wherever you like using the following approach:
就我而言,这个选项对我来说是正确的。
In my case this option works for me correctly.