2

Actually I have one custom TabBarController and I need to set badgeValue for separate tabbaritem at initially that custom tabar loaded with that index path of the tab bar item.

[[self navigationController] tabBarItem].badgeValue = @"3";

I used above code for that UIViewcontroller but it shows only when I click on the tab bar item.

and I have also tried the below code in custom tabBarController but it didn't working.

[[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];

I need to display the badge value at initial.

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
Hari Narayanan
  • 754
  • 1
  • 12
  • 36

2 Answers2

3

If you want to set badge number when you show first tab of your tabbarcontroller then set

  [[self.tabBarController.tabBar.items objectAtIndex:3] setBadgeValue:@"2"];

in viewdidload of your first tab, means first viewcontroller of your tabbbarcontroller

Ketan Parmar
  • 27,092
  • 9
  • 50
  • 75
  • can i declare it in didReceiveRemoteNotification method on appdelegate.m – Hari Narayanan Nov 17 '16 at 11:02
  • yes you can but for that you have to instantiate `tabbarcontroller` by setting `storyboard id` to your `tabbarcontroller` because you can not get `self.tabBarController` in appdelegate! – Ketan Parmar Nov 17 '16 at 11:28
0

Go to your AppDelegate where you are setting TabBarController as root view controller of UIWindow like this.

[self.windows setRootViewController:self.tabBarController];

Just write this code before setting the root view controller and after initialising the tabBarController object completely.

[[[[[self tabBarController] tabBar] items] objectAtIndex:3] setBadgeValue:@"2"];
Syed Qamar Abbas
  • 3,637
  • 1
  • 28
  • 52