自动更新UITabBar项目徽章值

时间:2014-08-25 12:09:05

标签: ios iphone uitabbaritem

在我的应用程序中有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秒按此顺序,所以我知道该方法被调用但是徽章价值未更新

2 个答案:

答案 0 :(得分:0)

调用您的函数以更新要在其中更新tabBarItem badgeValue的ViewController的“initWithCoder:”中的tabBarItem badgeValue。 TabBar加载时,将初始化与TabBarController中的选项卡关联的ViewControllers。我不知道你为什么打电话给viewDidLoadviewWillAppear两次。大多数徽章都会由NSNotificationCenterAPNS更新。

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]];
 }
}

答案 1 :(得分:0)

最终,最终起作用的是将NSTimer用这种方法:

-(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

的视图控制器的-(void)awakeFromNib方法