14

I've read this article: Change the text of a UILabel (UIBarButtonItem) on a toolbar programmatically

But it seems not working for my auto-hide toolbar?
I also tried barbuttonItem.title to set the text, failed too.

Any idea?

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
Kordan Ou
  • 1,502
  • 3
  • 15
  • 25

6 Answers6

16

Are you creating the button in Interface Builder? Make sure that the identifier is set to Custom, otherwise you will not be able to change the title.

Select the UIBarButtonItem in Interface Builder, open the inspector (Command + Shift + I), and select "Custom" under the dropdown next to Identifier.

Dheeraj D
  • 4,386
  • 4
  • 20
  • 34
Pyro2927
  • 1,112
  • 9
  • 18
  • Does your "set to Custom" mean "initWithCustomView"? If so, my item was already set to Custom. like this: percentage = [[UILabel alloc] init]; percentage.text = @"HELLO"; UIBarButtonItem *percentageBarItem = [[UIBarButtonItem alloc] initWithCustomView:percentage]; I want to keep it changeable after putting it on the toolbar. – Kordan Ou Apr 22 '10 at 16:54
  • sorry, I don't know how to make up in comment.../_\ – Kordan Ou Apr 22 '10 at 16:57
  • 1
    If you are simply using text, I would recommend calling - (id)initWithTitle:(NSString *)title style:(UIBarButtonItemStyle)style target:(id)target action:(SEL)action Then you can change the title later with ease. – Pyro2927 Apr 22 '10 at 17:00
  • Thanks! It works perfectly. Could I ask one more question? Howto change the value of UIBarButtonItem slider in the same condition(i.e., auto-hide toolbar) – Kordan Ou Apr 23 '10 at 02:25
8

Try this:

UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(yourMethod:)];
Ambili B Menon
  • 1,249
  • 2
  • 14
  • 26
4
  1. Create an IBOutlet for the UIBarButtonItem , outToMyButton in this example
  2. synthesize in .m
  3. outToMyButton.title = [NSString stringWithFormat:@"%i",myInt]; // or any NSString init
Dheeraj D
  • 4,386
  • 4
  • 20
  • 34
epee center
  • 49
  • 1
  • 1
4

Changing the title of a UIBarButtonItem programmatically:

@interface YourController : UIViewController
{
  UIBarButtonItem *barButton;
}

- (void)viewDidLoad
{
  barButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Set", @"Set")
                                               style:UIBarButtonItemStylePlain
                                              target:self
                                              action:@selector(barButtonClicked)];
}

And the BarButtonItem action:

- (void)barButtonClicked
{
  if([barButton.title isEqualToString:@"Set"])
  {
    //Enter Code
    barButton.title = @"Done"
  }
  else
  {
    //Enter Code
    barButton.title = @"Set"
  }
}
Bruno Bieri
  • 9,724
  • 11
  • 63
  • 92
Sanju
  • 1,148
  • 11
  • 26
2

The question was changing the text of UIBarButtonItem programmatically :

So here is how you do it. an example would be putting the weather info into the bar.

import UIKit
...

@IBOutlet weak var weatherBarLabel: UIBarButtonItem!


//then
func setTheWeatherInfo(weather: WeatherModel){
 weatherBarLabel.title = "\(weather.temperature)°"
}
Mr T
  • 1,409
  • 1
  • 17
  • 24
0

I believe that my problem was similar -- but not identical -- to yours: I wanted the text on an accessory toolbar to reflect selections of the last picker component.

I was, however, unable to change the text of a UILabel added to a UIBarButtonItem that is part of an inputAccessoryView displayed over a multiple-component sliding UIPickerView for each of several data-entry UITextFields. These elements are created programmatically (Xcode v 4.3 (4E109)), except for the text fields, each of which is created in the Storyboard and declared as a property.

Neither assigning the text to the toolbarTitle label, and then alloc/init-ing the bar button item with toolbarTitle as a custom view:

toolbarTitle.text = @"myTextFromPickerComponent";
UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithCustomView:toolbarTitle];

//nor alloc/init-ing the bar button item directly, passing the new text to initWithTitle:

UIBarButtonItem *title = [[UIBarButtonItem alloc] initWithTitle:toolbarTitle.text style:UIBarButtonItemStyleDone target:self action:nil];

Gave the desired behavior; i.e., the label text did not change, even though I confirmed with NSLog that I indeed was sending the new text.

I got a hint from the Docs under Custom Views for Data Input, Input Views and InputAccessory Views:

"The first responder can reload the input and accessory views by calling the reloadInputViews method of UIResponder."

I implemented this by having the current UITextField call reloadInputViews, and this worked for me with either of the two code fragments given above.

TheTiger
  • 13,264
  • 3
  • 57
  • 82