Reputation: 1682
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?
Upvotes: 14
Views: 24226
Reputation: 1168
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"
}
}
Upvotes: 4
Reputation: 1499
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)°"
}
Upvotes: 2
Reputation: 1112
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
.
Upvotes: 16
Reputation: 49
IBOutlet
for the UIBarButtonItem
, outToMyButton in this example.m
outToMyButton.title = [NSString stringWithFormat:@"%i",myInt]; // or any NSString init
Upvotes: 4
Reputation: 1249
Try this:
UIBarButtonItem *clearButton = [[UIBarButtonItem alloc] initWithTitle:@"Clear" style:UIBarButtonItemStyleBordered target:self action:@selector(yourMethod:)];
Upvotes: 8
Reputation: 29
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.
Upvotes: 0