开发者

Best way to update badgeValue of UITabBarController from a UIView

I have a tabBarController set up in the AppDelegate and have a few UIViewControllers with Nav Controllers. In one of the TabBar items, after I have pushed a few UIViews I want to update the badgeValue item of a different TabBar item.

Whats the best way to do this? The only way I can really think is a NSNotification and a singleton storage for the value, but it seems a lot of work for something开发者_JAVA技巧 simple, that and I have no idea about NSNotifications.

I had a wild guess at something like super.tabBarController.otherView.tabBarItem.badgeValue = @"1" (as I set which tab is selected in a similar way) but I'm not surprised this doesn't work.

Thanks


Thanks to alku83 who pointed me in the right direction the code is:

[[super.tabBarController.viewControllers objectAtIndex:2] tabBarItem].badgeValue = @"1";


I'm using Xcode 4.5 with Storyboards and iOS 6, so the answer may have changed since it was originally posted.

First you have to access the Tab Bar Controller like this:

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

Then you can set the badge like so:

[[tabController.viewControllers objectAtIndex:1] tabBarItem].badgeValue = @"New!";


I don't have the code on hand right now, but it should be something more like

...otherViewController.tabBarItem.badgeValue = 1;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜