0

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

Code cracker
  • 3,105
  • 6
  • 37
  • 67
shreyashirday
  • 892
  • 1
  • 10
  • 34
  • Try this, [[[[[self tabBarController] tabBar] items] objectAtIndex:tabIndex] setBadgeValue:badgeValueString]; Hope this will help. – josh Aug 25 '14 at 12:23

2 Answers2

0

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]];
 }
}
iAhmed
  • 6,556
  • 2
  • 25
  • 31
0

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

shreyashirday
  • 892
  • 1
  • 10
  • 34