之前一直以為傻傻在當(dāng)前的controller中寫
self.tabBarItem.badgeValue = @"1";
其實是要在在tabbar的第一個controller里面寫
- (void)setMsgBadgeWithNum:(int)num{
UIViewController *tController = [self.tabBarController.viewControllers objectAtIndex:2];int? ? badgeValue = [tController.tabBarItem.badgeValue intValue];tController.tabBarItem.badgeValue = [NSString stringWithFormat:@"%d",badgeValue+num];
}
當(dāng)num=3時瓦宜,有如下效果
這樣第一次加載時tabbar時俄周,badge就有了值了。而一般點擊后badge會清空跋核,這個效果只需要在當(dāng)前controller的didload中寫如下代碼:
self.navigationController.tabBarItem.badgeValue = nil;