Change title of UITabBarItem dynamically - iphone

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.

Related

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.

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";
}

Act on click of a button on the Nav Bar for moreNavigationController -- Can't pushviewcontroller

Okay, here is my issue: My app has a display of categories in the tab bar at the bottom of the iPhoneOS screen. This only allows 5 categories before it presents the MORE button. I have over 25 (please do not answer this by saying: "Rethink your application...etc" -- that was rudely said before. They are food, drink, etc categories and cannot be changed). I want to allow the user to put their favorites on the home page. The Apple moreNavigationController editing system only allows 20 tab bar items to be rearranged due to space constraints on the editing page. This is not enough so i need to implement my own Editing screen. I set the rightBarButtonItem to nil and created my own. Using NSLog, i can see the "click" happens when clicking the EDIT button, but I cannot push using pushViewController. Nothing happens. I think it has something to do with the navigationController I am addressing...but i am not sure. ps: This all happens in my App Delegate which DOES act as both UITabBarControllerDelegate & UINavigationControllerDelegate.
I tried to do the following:
- ( void )navigationController:( UINavigationController * )navigationController_local willShowViewController:( UIViewController * )viewController_local animated:( BOOL )animated
{
UIViewController * currentController = navigationController_local.visibleViewController;
UIViewController * nextController = viewController_local;
// Do whatever here.
NSLog(#"Nav contoller willShowViewController fired\n'%#'\n'%#'\nThere are currently: %d views on the stack\n",currentController,nextController,[self.navigationController.viewControllers count]);
if ( [nextController isKindOfClass:NSClassFromString(#"UIMoreListController")])
{
UINavigationBar *morenavbar = navigationController_local.navigationBar;
UINavigationItem *morenavitem = morenavbar.topItem;
morenavitem.rightBarButtonItem = nil;
NSLog(#"Is a UIMoreListController\n");
UIBarButtonItem *editTabBarButton = [[UIBarButtonItem alloc]
initWithTitle:#"Edit"
style:UIBarButtonItemStylePlain
target:self
action:#selector(editTabBar:)];
morenavitem.rightBarButtonItem = editTabBarButton;
[editTabBarButton release];
}
}
This works to place an EDIT button at the top right of the screen -- mimicking Apple's look and feel... but when that button is clicked, you cannot exit the darn moreNavigationController.
I have tried many things. UIAlerts work, etc...but pushing (or popping -- even popping to root view) a view controller on the stack does not.
- (void) editTabBar:(id)sender {
NSLog(#"clicked edit tabbar\n");
NSLog(#"Total count of controllers: %d\n",[self.navigationController.viewControllers count]);
TabBarViewController *tabBarViewController2 = [[TabBarViewController alloc] initWithNibName:#"TabBarView" bundle:nil];
tabBarViewController2.navigationItem.title=#"Edit Tab Bar";
[self.navigationController pushViewController:tabBarViewController2 animated:YES];
[tabBarViewController2 release];
NSLog(#"finished edit tabbar\n");
}
If you click the edit button on the moreNavigationController's display page, you get the log entries like expected AND (this is strange) the views on the stack climbs -- but no page change occurs. I marked it down to not using the correct navigation controller...but I am lost on how to find which one TO use.
this is a weird one too. In the edit function if i just do this:
- (void) editTabBar:(id)sender {
self.tabBarController.selectedIndex = 0;
}
It DOES take me home (to tabbarcontroller 0)
BUT doing this:
- (void) editTabBar:(id)sender {
[self.navigationController popToRootViewControllerAnimated:YES];
}
does not work.
Does the moreNavigationController have some special quality that screws with the rest of the system?
I would try reimplementing the whole "More" functionality from scratch. In other words, store the four home tabs in your user defaults and add a dummy fifth tab that switches to your own complete reimplementation of the more view controller stack.
You could even write a lightweight subclass of UITabBarController that handled this for you.
UITabBarController is evil, so I wouldn't be at all surprised if MoreController had some special properties, too.
I have had success intercepting the More Controller in shouldSelectViewController to change the data source; you may be able to find some workaround there.
PS I am inclined to agree that you could consider redesigning your app so that you didn't need an unlimited number of viewControllers attached to the tab bar just to select categories; you might have better luck using a tool bar with a single, scrollable, custom view in it. If that's really the best way of picking categories for your app, of course.

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

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];