【发布时间】:2014-08-25 12:09:05
【问题描述】:
在我的应用程序中有 UITabBar 项目,我希望标签栏项目的徽章值每 X 秒更新一次,但我无法弄清楚...
这是我的更新方法:
-(void)updateTabBadgeValue{
NSLog(@"tick");
if([PFUser currentUser]!=nil){
NSLog(@"user is not null");
UIStoryboard *mySb = [UIStoryboard storyboardWithName:@"Main_iPhone" bundle:nil];
UITabBarController *myTbc = [mySb instantiateViewControllerWithIdentifier:@"tbc"];
NotificationNavigation *nn = [myTbc viewControllers][2];
NotificationViewController *nvc = [nn viewControllers][0];
[nvc awakeFromNib];
PFQuery *query = [PFQuery queryWithClassName:@"NoCo"];
[query whereKey:@"username" equalTo:[[PFUser currentUser]username]];
PFObject *noco = [query getFirstObject];
if([[noco objectForKey:@"count"] intValue] > 0){
NSLog(@"tock");
[nn.tabBarItem setBadgeValue:[NSString stringWithFormat:@"%d",[[noco objectForKey:@"count"] intValue]]];
[myTbc viewDidLoad];
[myTbc viewWillAppear:YES];
[nvc viewDidLoad];
[nvc viewWillAppear:YES];
}
}
}
我这样实现这个方法:
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
[NSTimer scheduledTimerWithTimeInterval:5.0 target:self selector:@selector(updateTabBadgeValue) userInfo:nil repeats:YES];
}
但它似乎不起作用...我的日志输出显示“tick”、“user is not null”、“tock”,每 5 秒按此顺序显示,所以我知道正在调用该方法,但是徽章值未更新
【问题讨论】:
-
试试这个,[[[[[self tabBarController] tabBar] items] objectAtIndex:tabIndex] setBadgeValue:badgeValueString];希望这会有所帮助。
标签: ios iphone uitabbaritem