-1

Im developing an iOS app using objective c. in my app if user is not register then register view controller is rootViewController. if user is register then tabBarController having three tabs is rootViewController. i have to set tabBarItem badge value from any view controller. suppose if i am on third tab and it is having segue with another view controller and i am on that view controller, i have to change the tabBarItem badge value of first view controller from here. in my case the tabBarItem badge value update only i go to that tab as i am using

NSString *upcomingcount=[NSString stringWithFormat:@"%lu",(unsigned long)self.arrbadge.count];
            self.navigationController.tabBarItem.badgeValue=upcomingcount;

in viewwillappear.

is there any way to set the badgeValue from any ViewController? i want to update badge value from any ViewController

Suraj Sukale
  • 1,778
  • 1
  • 12
  • 19
goks
  • 1
  • 3

3 Answers3

1

plz use this method in your app delegate

 - (void)update_badgeWithViewControllerIndex:(NSInteger)ViewControllerIndex {


            UITabBarController *tabBarController =(UITabBarController*)[[(AppDelegate*)
                                                                         [[UIApplication sharedApplication]delegate] window] rootViewController];


              // Set the tab bar number badge.
              UITabBarItem *tab_bar = [[tabBarController.viewControllers objectAtIndex:ViewControllerIndex] tabBarItem];

              // Show the badge if the count is
              // greater than 0 otherwise hide it.

              if ([badgeValue > 0) {
                  [tab_bar setBadgeValue:badgeValue]; // set your badge value
              }

              else {
                  [tab_bar setBadgeValue:nil];
              }



    }

for use this method in every viewController create

@property (nonatomic,strong) AppDelegate *appDelegate;




self.appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

  [self.appDelegate update_badgeWithViewControllerIndex:yourViewControllerIndex];
balkaran singh
  • 2,754
  • 1
  • 17
  • 32
  • not working!! still badge updates only if we go on that view controller. thanks for the response – goks Jul 28 '16 at 10:46
  • in fact i have tried the other way round also. created protocol of view controller and your method and delegate the method to AppDelegate.m , include .h file of my current view controller in AppDelegate.h declare the protocol and now xcode giving the strange error "" Cannot find protocol declaration for .......""". – goks Jul 28 '16 at 10:53
0

You can use KVO to observe the change in your upcomingcount and update your badge value.

    -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context{

        if ([keyPath isEqualToString:NSStringFromSelector(@selector(upcomingcount))]) {
            //SET BADGE VALUE HERE

        }

}
Vishal Sonawane
  • 2,637
  • 2
  • 16
  • 21
0
func setBadge(){
        let viewconrollers = self.tabBarController?.viewControllers
        viewconrollers![3].tabBarItem.badgeValue = "Your String value"
        viewconrollers![3].tabBarItem.badgeColor = UIColor.green
    }


//call setBadge() method from view controller's viewdidload method.

// select your give number it the view controllers array for providing badge on the desired tab 
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Raghib Arshi
  • 717
  • 8
  • 12