Reputation: 14886
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.
Upvotes: 2
Views: 5678
Reputation: 1
In my case this option works for me correctly.
[[[[[self tabBarController] tabBar] items] objectAtIndex:1] setBadgeValue:@""];
Upvotes: 0
Reputation: 131
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";
}
}
Upvotes: 2
Reputation: 664
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"];
Upvotes: 0
Reputation: 135548
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?
Upvotes: 13
Reputation: 14886
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.
Upvotes: 0