0

I have 2 view controllers in a tab bar controller. My 2nd Nav Controller wants to set a badge value. This controller is not loaded when the app starts, so the badge does not show. If I go over to that tab, the badge is properly updated.

this snippet runs when the tab's View Controller loads/reloads/updates/etc...

[self.navigationController.tabBarItem setBadgeValue:[NSString stringWithFormat:@"%u",[self.photos count]]];

Is the correct way to do this: Override the Nav Controller with a custom class and put the badge value in at that level? It seems like that is where I should put this info, but I haven't found a definite answer.

kejoki
  • 93
  • 9
  • Where is your 2nd _View_ Controller getting it's data from? What is `self.photos`? Is there any way you can get at that same data from your first view controller? – foundry Mar 27 '13 at 18:25
  • The 2 view controllers really shouldn't see each other's data. They are dissimilar, self-contained applications. The self.photos information is ultimately coming out of a user's defaults list, but I seem to keep thinking that it would be "bad" to have that 1st view controller grab a piece of data that it shouldn't really know anything about. – kejoki Mar 27 '13 at 19:50
  • It's not strictly relevant but... are you using storyboard, xib or just code? – foundry Mar 27 '13 at 19:57
  • and... just to be clear... you have a UITabBarController, with two items, and each item's initial UIViewController is embedded in a separate UINavigationController? – foundry Mar 27 '13 at 19:59
  • Yes, it is in a storyboard and just like you said. UITabBarController going to 2 different UINavigationCollers that have viewcontrollers embedded in each of them. – kejoki Mar 27 '13 at 22:35

1 Answers1

1

When the TabBarController is loaded, all of it's contained initial viewControllers are initialised. But their views are not loaded until you navigate to the respective tab item. So you can't execute code at this point in any of the view-loading methods (viewDidLoad etc). However you can execute code by overriding one of the initialisation methods.

If using storyboards the process of unarchiving the viewController triggers this method when the NIB has loaded:

- (void) awakeFromNib
{

}

If not using Storyboards, this initialiser is called prior to NIB loading:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

In either case you can override these methods to execute some code...

- (void) awakeFromNib
    [super awakeFromNib];
    [self.navigationController.tabBarItem 
           setBadgeValue:[NSString stringWithFormat:@"badgeValue"]]];
}


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        [self.navigationController.tabBarItem 
           setBadgeValue:[NSString stringWithFormat:@"badgeValue"]]];
    }
    return self;
}

However you will need to take care where you are getting your data from. At this point self.photos may be uninitialised for example. If the data for this is coming out of userdefaults, you should be able to read those in here and set your badge accordingly.

foundry
  • 31,615
  • 9
  • 90
  • 125
  • Yes - that is working. Is there a [super awakeFromNib] that needs to be called before executing any of my code? – kejoki Mar 27 '13 at 22:37
  • @kejoki - yes, I've added a [super awakeFromNib].. in case the `super` is doing something with it. – foundry Apr 14 '13 at 15:23