Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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???

like image 374
saurabh Avatar asked Nov 14 '22 01:11

saurabh


1 Answers

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.

like image 108
XJones Avatar answered Jan 02 '23 21:01

XJones