iphone code - change the tabBar badge value from the viewController's - iphone

I have a UITabBarController,
How can I create/update the badge value of the tabBar item from my viewController ?
The tabBar item's created in the ib.
I connected the tabBar item to the controller using an IBOutlet UITabBar *tabBar.
thanks.

If your viewcontroller already has a tab bar controller associate with it, you can just drill down to the tab bar item and set its badge, like this:
[[[[[self tabBarController] tabBar] items]
objectAtIndex:tabIndex] setBadgeValue:badgeValueString];
where tabIndex is the index of the tab item you want to set and badgeValueString is the string value you want to set on the tab.

Use the tabBarItem instance of your view controller to access the tab bar item directly instead of drilling down to it.
[self.tabBarItem setBadgeValue:badgeValue];

If there are three tabs in your app First, Second and Third
Tab index starts from 0
First tab index 0
Second tab index 1
Third tab index 2
If you want to set badge value 5 in Second tab
So pass 1(tab index of Second tab) for objectAtIndex:1 and pass 5(badge value) for setBadgeValue:#"5"
[[self.tabBarController.tabBar.items objectAtIndex:1] setBadgeValue:#"5"];
Also you can clear/remove badge value passing nil for spacific tab
[[self.tabBarController.tabBar.items objectAtIndex:1] setBadgeValue:nil];

int indexICareAbout = 2;
NSString *badgeValue = #"10";
[[[[[self tabBarController] viewControllers]
objectAtIndex: indexICareAbout] tabBarItem] setBadgeValue:badgeValue];

Related

iOS - incorrect badge value retrieved from tab

I'm trying to retrieve the value displaying for a specified tab. It was working before and it stopped for some reason? The int current_badge = line on the code below is where the badge value is fetched and returns zeros no matter what the badge value actually is. I'm in a viewcontroller segued off of a uitabcontroller. Anyone have any thoughts on what I'm doing wrong?
UPDATE: It appears to be because I'm in a viewcontroller off of a tab controller. Move the same code into a tab viewcontroller and it works fine. Is there a better way to determine the badge value in a tabcontroller when segued out of it?
-(void)badgeUpdate:(int)tab:(int)dayspan
{
// getting the current badge amount
int current_badge = [[[super.tabBarController.viewControllers objectAtIndex:tab] tabBarItem].badgeValue intValue];
// testing for badge level
if (current_badge > 0)
{
// testing for updates on tab for dayspan or longer
int updates_waiting = [self updatesWaitingCheck:tab:dayspan];
if (updates_waiting > 0)
{
// setting new badge level on tab
[[[[[self tabBarController] viewControllers] objectAtIndex:tab] tabBarItem] setBadgeValue:[NSString stringWithFormat:#"%d", updates_waiting]];
}
else
{
// turning off badge display
[[[[[self tabBarController] viewControllers] objectAtIndex:tab] tabBarItem] setBadgeValue:nil];
}
}
}
The controller for that tab is likely being deallocated when you switch away. I would suggest moving the logic to determine the current count out of the view layer, and into a higher level controller that persists across all tabs. Otherwise, you'll run into the problem you're seeing.
Most of the time, if you're looking for a data value in a view object, rather than in a controller or model somewhere, you're working against the framework and you'll run into problems like this.

Change title on "More" tab

Is there any way to change title on "More" tab (text on uitabbaritem)?
well..If you want to change the title of the navigation bar of your more tab use the method given by 7KV7
and if you want to change the tab title ...
there is an another way..
suppose you have eight tabs..
1) In your tab Bar controller ...make only five tabs...remove all the other views other than five tabs..
2)lets make your fifth tab as your "more tab"...take it as a tableviewController and in it's tableView elements add elements as your sixth..seventh..eighth tab's name and image...
3)in fifthViewController's rowdidselect method navigate the sixth , seventh and eighth tab's respective view controllers..
Pros:-you can change the name and the image of the more tab
Cons:-well you can not use the edit button of your more tab where you can change the tab element's order..
try it if you are comfortable with this..
Taken from this Link
Yes, it is possible.
UINavigationBar *moreNavigationBar = tabBarController.moreNavigationController.navigationBar;
moreNavigationBar.topItem.title = #"Your title";
tabBarController is your UITabBarController
In your UITabBarController you just add this simple line under the viewDidLoad and under superViewDidLoad:
self.moreNavigationController.navigationBar.topItem.title = #"Your Title";
So then it should look like this:
- (void)viewDidLoad {
[super viewDidLoad]; // Do any additional setup after loading the view.
self.moreNavigationController.navigationBar.topItem.title = #"Mer";
}
To make it worked I also created a new Objective-c class naming it: "MyTabBarController". And then I clicked my TabBarController in storyboard and set the class to: "MyTabBarController". When I pasted the code in the .m file it worked perfectly.

Change title of UITabBarItem dynamically

I have 9 tabs in my tabbar... And I want to change the title of all of them from some view controller. and I did it as follows:
for (int i=0; i(less than)[appDelegate.tabBarController.viewControllers count]; i++) {
UIViewController *uv=[appDelegate.tabBarController.viewControllers objectAtIndex:i];
uv.tabBarItem.title=#"test";
}
It changes the title for all visible tabs instantly but not working for tabs in more...
However if I click on edit button in more nav cntrl it shows changed name.
Also... very strange... If I select some tab in more then all the tabs reflects new name
why is it so???
Changing the title of a UIBarItem (superclass of UITabBarItem) needs to be done before the item is added to a bar per Apple docs. Looks like iOS is caching the titles once the items are added to the bar so you're getting unpredictable behavior.
From the UIBarItem Class Reference:
title
The title displayed on the item.
#property(nonatomic, copy) NSString *title
Discussion
You should set this property before adding the item to a bar. The default value is nil.

How to add a badge to a UITabBar that is customizable?

I am adding a badge to my UITabBarController's UITabBar as such:
UITabBarItem *tbi = (UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:1];
tbi.badgeValue = #"2";
However, my UITabBarController is customizeable, so the index may change. How can I make sure the badge gets applied to the correct UITabBarItem?
One suggestion you might consider is setting a tag on each tab bar item. You can do this in Interface Builder or when you create the item by code. You can then loop through the view controllers in the tab bar controller looking for the one with the tab bar item you are interested in. For example:
// #define MyTabBarItemTag 999
for (UIViewController *viewController in stTabBarController.viewControllers) {
if (viewController.tabBarItem.tag == MyTabBarItemTag) {
viewController.tabBarItem.badgeValue = #"2";
}
}
UITabBarItem *tbi = (UITabBarItem *)self.tabController.selectedViewController.tabBarItem;
tbi.badgeValue = #"New";
Also works.
Swift version:
self.tabBarController?.selectedViewController?.tabBarItem.badgeValue="12";
I'd use an NSMutableDictionary property on the class that owns the tab bar controller, associating tab names with positions, and a method to retrieve by name:
-(UITabBarItem*)getTabByName:(NSString*)tabName {
return [stTabBarController.tabBar.items objectAtIndex:[[tabDict valueForKey:tabName] intValue]];
}
Initialize the dictionary in your setup code for each tab, since you know the tab index at that time:
[tabDict setValue:[stTabBarController.tabBar.items objectAtIndex:1] forKey:#"myTabName"];
Keep a reference to the tab bar item that you want to modify.
EDIT as a response to your code request:
I believe that there is a single place in your app where you update the badges on the tab bar items. Just add an array of tab bar items (or separate tab bar items) as a member(s) of that class (+ properties if needed) and update the items directly without fetching from the current tab bar items list ((UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:1];).
For instance, if you decide to keep references to the tab bar items directly (without an array) then the code might look like that:
// Put the next code right after initiating the tab bar and/or after adding new tab bar items to it...
self.newsTabBarItem = (UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:1];
self.friendsTabBarItem = (UITabBarItem *)[stTabBarController.tabBar.items objectAtIndex:2];
// etc.

Change title of UItabbaritem in other view?

How do I change the UITabbaritem titles when starting the App?
I got a UITabBar with 4 tabs. I want the user to be able to change between two different languages. If the user chooses a different language I need to set a different title to the UITabbaritems. I know I can use self.title = #"title"; but that only changes the current tabbaritem title. How can I change all the titles at once, on load, and when choosing a different language?
I got the same problem and solved it with.
[[self.tabBarController.viewControllers objectAtIndex:index] setTitle:#"Your title"];
Where index is your UITabBarItem index.
You need to store all your UITabBarItem into an array, when the user tap the button, you need to loop through that array and set the title.
for (UITabBarItem *item in items) {
item.title = #"WHATEVER HERE";
}