问题
In my app there UITabBar items, I want the badge value of the tab bar item to be updated every X seconds, but I can't quite figure it out...
Here is my method for updating:
-(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];
}
}
}
and I implement this method like this:
- (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];
}
but it doesn't seem to be working...my log output shows "tick", "user is not null", "tock", in that order every 5 seconds so I know the method is being called but the badge value is not being updated
回答1:
Call your function to update the tabBarItem badgeValue in 'initWithCoder:' of the ViewController whose tabBarItem badgeValue you want to update. The ViewControllers that are associated with tabs in the TabBarController are initialized when the TabBar loads. i donot know why are you calling viewDidLoad
and viewWillAppear
twice. Mostly badges are updated by NSNotificationCenter
or APNS
though.
for (UIViewController *viewController in myTbc.viewControllers) {
if (viewController.tabBarItem.tag == MyTabBarItemTag)
// give tag of ur tabbar item for which you want to update badge Or you can also use index of item here.
{
viewController.tabBarItem.badgeValue =@"1" //[NSString stringWithFormat:@"%d",[[noco objectForKey:@"count"] intValue]];
}
}
回答2:
Ultimately, what ended up working was putting the NSTimer with this method:
-(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");
//for(UIViewController *controller in myTbc.viewControllers){
//if(controller.tabBarItem.tag == 101){
[self.tabBarItem setBadgeValue:[NSString stringWithFormat:@"%d",[[noco objectForKey:@"count"] intValue]]];
//[myTbc viewDidLoad];
//[myTbc viewWillAppear:YES];
//[nvc viewDidLoad];
//[nvc viewWillAppear:YES];
//}
//}
}
else{
NSLog(@"pause");
}
}
}
into the -(void)awakeFromNib
method of the view controller that had the tabBarItem
来源:https://stackoverflow.com/questions/25485613/auto-update-uitabbar-item-badge-value