自动更新UITabBar项目徽章值 [英] Auto-update UITabBar item badge value
问题描述
在我的应用程序中有UITabBar项,我希望标签栏项的徽章值每X秒更新一次,但我不太清楚...
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...
这是我的更新方法:
-(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秒按此顺序进行,所以我知道该方法正在被调用,但是徽章值未更新
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
推荐答案
最终,最终有效的方法是使用以下方法放置NSTimer:
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");
}
}
}
进入具有tabBarItem
into the -(void)awakeFromNib
method of the view controller that had the tabBarItem
这篇关于自动更新UITabBar项目徽章值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!