views:

878

answers:

4

I have used the Tab Bar Application in New Project in Xcode. In IB, I have added NavigationControllers and I have five in total. On app launch the user login, and I would like to set a badgeValue for the fifth tabBarItem, but I am unable to do so, pretty much anywhere.

I'd prefer to do it after the login code has run, but I am not sure how I tell the fifth viewController to get the badgeValue. Also, I have tried it inside the viewController but self.tabBarItem.badgeValue doesn't work either.

+4  A: 

The view controller must have been created for this to work. The only other thing I can think of is that you have to access the tab bar item of your navigation controller and not of the navigation controller's root controller. So from the nav controller's root controller class, this should work:

self.navigationController.tabBarItem.badgeValue = @"...";

Or, from applicationDidFinishLaunching:

[(UIViewController *)[tabBarController.viewControllers objectAtIndex:4] tabBarItem].badgeValue = @"...";

Does any of this not work?

Ole Begemann
Hmm. At least I can get something now using the first piece of code. However, still not in the correct TabBar (unless I do it from the viewController itself). With the second line, which looks correct, I get an error: "request for member 'navigationController' in something not a structure or union"
Canada Dev
Really? The second line doesn't even mention navigationController? Where would this error message come from?
Ole Begemann
Sorry, no I tried with and without ...objextAtIndex:4].navigationController.tabBarItem and neither worked. Same error that there's no member.
Canada Dev
If you get the error that tabBarItem is not a member try casting the result from (UIViewController*)[...objectAtIndex:4]
Felix
[(UIViewController*)[tabBarController.viewControllers objectAtIndex:4] tabBarItem].badegValue
Felix
Hi Felix. That totally worked! I found a solution myself last night, but it required three lines of code, so yours is better. Can you post your last comment as an answer. This way I can mark is an as answer and you'll get the points.
Canada Dev
Good catch, Felix. I have also edited my answer above to include the typecast but of course you should get the points for the correct answer.
Ole Begemann
Gave Ole the correct answer, but if you add yours Felix, I'll set yours an an answer too.. If it's possible :)
Canada Dev
A: 

Not a perfect solution, but I found this to work:

self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:4];
self.tabBarController.selectedViewController.tabBarItem.badgeValue = @"1";
self.tabBarController.selectedViewController = [self.tabBarController.viewControllers objectAtIndex:0];

SelectedViewController is 0 by default, and I could not set the badgeValue for anything but the selectedViewController. Therefore, if I set the selectedViewController to the desired on, then set it's badgeValue and then set the selectedViewController back to 0, it will not mess up the app's launch and everything will work fine.

Canada Dev
A: 

The issue with setting the badgeValue with this approach:

[(UIViewController *)[tabBarController.viewControllers objectAtIndex:4] tabBarItem].badgeValue = @"...";

is if you are using the More feature when you have more than 5 tab items. If the user moves the tab item in question to a different position you'll be setting the badge value to a different tab item.

If you have started with Apple's navigation/tab controller template, simply create an IBOutlet in the Application Delegate linking to the specific tab item you wish to update.

Then access your tab item from application delegate from wherever you like using the following approach:

MyApplicationDelegate *appDelegate = (MyApplicationDelegate *)[[UIApplication sharedApplication] delegate];
[[appDelegate myTabItemOutlet] setBadgeValue:@"1"];
Cocoanut
+1  A: 

If you set a tag for the tabBarItem ([UITabBarItem initWithTitle:image:tag:]) and you don't want to create an IBOutlet for finding the right badgeValue, you can use this tag to ensure setting the right value:

for (UIViewController *viewController in self.tabBarController.viewControllers) {
    if (viewController.tabBarItem.tag == 1) {
        viewController.tabBarItem.badgeValue = @"1";
    }
}
Florian