32727

Auto-update UITabBar item badge value

Question:

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

Answer1:

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

Answer2:

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

Recommend

  • Retrieve Image for different Class Data browser Parse.com
  • How to implement friend add/remove functionality in an iOS app using parse database?
  • Add more button at the end of text inside UITextView Objective-C?
  • Segue Won't Pass Data
  • Multiple Query Search with Parse.com
  • Comma-formated numbers in jQuery
  • 2D convolution with a with a kernel which is not center originated
  • Validating numeric formats in Win Forms
  • sql server datetime
  • Tensorflow: Cannot interpret feed_dict key as Tensor
  • How to parse amcharts stock dates
  • Music played using asyncTask isn't stopping by using cancel
  • How to normalize coordinates inputs for neural networks
  • Parse.com for Xcode to make simple app like trivia crack type
  • Getting the system's time zone with Dragon NaturallySpeaking's Advanced Scripting
  • What are the parameters of TensorFlow's dynamic_rnn for this simple data set?
  • Can't approximate simple multiplication function in neural network with 1 hidden layer
  • scikit-neuralnetwork mismatch error in dataset size
  • PFQueryTableViewController text of cell issue
  • PFObject compare:options:range error when searching parse.com class
  • Multi-Classification NN with Keras error
  • How to use previous output and hidden states from LSTM for the attention mechanism?
  • What is the best method to exclude data and query parts of data in a Swift Firebase query?
  • Prime numbers using Sieve of Atkin with BigInteger
  • Firebase query with wrong data doesn't give any call back [duplicate]
  • Swift - Search data not loading
  • Parse cloud code ascending / limit won't work in query in promise
  • ios swift parse: Collect data from 3 classes
  • Storing command output lines into array based on new line character
  • didSelectRowAtIndexPath method at PFQueryTableViewController
  • Parse.com relational query issue
  • SCSS Language Injection in Pycharm
  • ActiveRecord query for a count of new users by day
  • Can you perform a UNION without a subquery in SQLAlchemy?
  • PostgreSQL Query without WHERE only ORDER BY and LIMIT doesn't use index
  • Display Images one by one with next and previous functionality
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • SQL merge duplicate rows and join values that are different
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • Getting error when using KSoap library to consume .NET web services