13

我在 AppDelegate 中设置了一个 tabBarController,并有一些带有导航控制器的 UIViewController。在其中一个 TabBar 项目中,在我推送了几个 UIView 之后,我想更新另一个 TabBar 项目的 badgeValue 项目。

最好的方法是什么?我真正能想到的唯一方法是 NSNotification 和值的单例存储,但对于一些简单的事情似乎需要做很多工作,而且我不知道 NSNotifications。

我对 super.tabBarController.otherView.tabBarItem.badgeValue = @"1" 之类的东西有一个疯狂的猜测(因为我以类似的方式设置了选择哪个选项卡),但我并不惊讶这不起作用。

谢谢

4

3 回答 3

38

感谢 alku83 指出我正确的方向,代码是:

[[super.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue = @"1";
于 2010-05-18T07:55:35.257 回答
7

我正在使用带有 Storyboards 和 iOS 6 的 Xcode 4.5,所以答案可能自最初发布以来已经改变。

首先,您必须像这样访问标签栏控制器:

UITabBarController *tabController = (UITabBarController *)self.window.rootViewController;

然后你可以像这样设置徽章:

[[tabController.viewControllers objectAtIndex:1] tabBarItem].badgeValue = @"New!";
于 2012-09-29T15:31:20.297 回答
0

我现在手头没有代码,但它应该更像

...otherViewController.tabBarItem.badgeValue = 1;
于 2010-05-18T03:36:46.003 回答