0

I have a tab bar controller, that has 3 view controller attached.

Say you start up the app, the first view controller is the only view out of the other three that is loaded. I want set the badge value of the third view controller that isn't loaded. This is because I want the user to know they have notifications when they open the app.

Is it possible and how can I do it? I have tried the following:

Calling the view ( the one that I want to change its badge ) from another class:

 NotificationsViewController *class = [NotificationsViewController alloc]init]; 

[class view];

Then, In the NotificationsViewController class, I set the badge to 1 if the app has any notifications using parse:

- (void)viewDidLoad {
[super viewDidLoad];

PFInstallation *currentInstallation = [PFInstallation currentInstallation];
if (currentInstallation.badge > 0) {

    NSString *badge = @"1";

        [self.tabBarItem setBadgeValue:badge];


    }
   }

Why isn't this working? How can I change the badge of the view controller that isn't loaded yet?

rici
  • 234,347
  • 28
  • 237
  • 341
Josh
  • 745
  • 1
  • 7
  • 22
  • 1
    You're creating a new instance of your controller and setting the badge on it. You need to access the one that's in your tab bar controller. – dan Jul 17 '15 at 05:04
  • @dan could you please provide me some sample code for that? I'm truly lost... – Josh Jul 19 '15 at 10:26

1 Answers1

0

First, you need to get your tab bar item:

UITabBarItem *tabBarItem = [[self.tabBarController.viewControllers objectAtIndex:YOUR_INDEX] tabBarItem];

Then, simply set the badge on it.

NSString *badge = @"1";
[tabBarItem setBadgeValue:badge];
eschanet
  • 1,063
  • 6
  • 15
  • Does the view have to be loaded first? – Josh Jul 18 '15 at 03:26
  • No absolutely not, the tabbar item is not directly related to a view, you can always grad the tabbar item and set a badge to it without loading the viewcontroller. – eschanet Jul 18 '15 at 20:16
  • Hmm Because I get a error relating to the `self.tabBarController`, as I am calling this code from my app delegate? Do I need to change this code if I want to call it from my app delegate? – Josh Jul 18 '15 at 22:16
  • So you have a tabbar controller that you're surely initializing in your appdelegate. You only need to call that controller. How are you initializing your tabbar controller? – eschanet Jul 18 '15 at 22:24
  • This is the code I call: `TabBarViewController *class = [[TabBarViewController alloc]init]; UITabBarItem *tabBarItem = [[class.tabBarController.viewControllers objectAtIndex:2] tabBarItem]; NSString *badge = @"1"; [tabBarItem setBadgeValue:badge];` – Josh Jul 18 '15 at 22:48
  • Are your creating a new instance of your tabbar controller there? You need to grab the tabbar that is shown in your app, not a new one. Please post the code where you populate the tabbar controller. – eschanet Jul 19 '15 at 08:54
  • I have no idea if I am creating a new instance, thats all the code I use to try change the badge value? I did create a custom class for the tab bar controller but I didn't alter it at all??? – Josh Jul 19 '15 at 10:27
  • In your AppDelegate has to be a part of code where you initiate your tabbarcontroller, could you please show that part? – eschanet Jul 19 '15 at 17:34
  • Could you provide example code for what you are talking about? As I use a storyboard Tab Bar Controller, I don't believe you need to do any coding or initialisation to get a tab bar controller working, so I am lost at what you are talking about? This should be so simple yet it is proving to be extremely ambiguous as to why it isn't working?! – Josh Jul 20 '15 at 07:53