Uibarbuttonitem Changing Title Not Working

UIBarButtonItem changing title not working

I've done the following to dynamically change the title of a UIBarButtonItem. In this situation I am not using a UIViewTableController and cannot use the standard editButton. I have a view with a tableView as well as other subviews and wanted to emulate the behavior of the limited UIViewTableController.

- (void)InitializeNavigationItem
{
NSMutableArray *array = [[NSMutableArray alloc] initWithCapacity:2];
UIBarButtonItem* barButton;

barButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd
target:self
action:@selector(addNewItem:)];
barButton.style = UIBarButtonItemStyleBordered;
[array addObject:barButton];

// --------------------------------------------------------------------------------------

barButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style:UIBarButtonItemStyleBordered
target:self
action:@selector(editMode:)];
barButton.style = UIBarButtonItemStyleBordered;
barButton.possibleTitles = [NSSet setWithObjects:@"Edit", @"Done", nil];
[array addObject:barButton];

self.navigationItem.rightBarButtonItems = array;
}

- (IBAction)editMode:(UIBarButtonItem *)sender
{
if (self.orderTable.editing)
{
sender.title = @"Edit";
[self.orderTable setEditing:NO animated:YES];
}
else
{
sender.title = @"Done";
[self.orderTable setEditing:YES animated:YES];
}
}

Note that I didn't use the the UIBarButtonSystemItemEdit barButton, you cannot manually change the name of that button, which makes sense.

You also might want to take advantage of the possibleTitles property so that the button doesn't resize when you change the title.

If you are using a Storyboard/XIB to create/set these buttons, ensure that the Bar Button Item Identifier is set to Custom for the button which you'd want to control the title for.

Setting UIBarButtonItem not changing title, changing only style

Just do this

UIBarButtonItem *barButton = [[UIBarButtonItem alloc] initWithTitle:@"Edit" style: UIBarButtonItemStyleBordered target:self action:@selector(edit:)];
barButton.tag = 0;
barButton.possibleTitles = [NSSet setWithObjects:@"Edit", @"Done", nil];

Just change state according to click. simple thing.

-(void) edit:(UIBarButtonItem *) barBtnItem
{
if (barBtnItem.tag == 0)
{
barBtnItem.tag = 1;
[self.navigationItem.rightBarButtonItem setTitle:@"Done"];
}
else
{
barBtnItem.tag = 0;
[self.navigationItem.rightBarButtonItem setTitle:@"Edit"];
}
}

Why won't my UIBarButtonItem in my navigation toolbar change titles?

So I was able to reproduce your issue on my own project and I have it running correctly.

It is possible that your issue lies in the System Item type of the button you have added to the Toolbar. Go to storyboards, select your button, and make sure you set its System Item to Custom in the Attributes Inspector, instead of Edit. Initially, I tried using Edit but its just not as simple to change the title as if you have it set as Custom.

After this I created an outlet and an action from the button, just as you did in your code.

  1. Here's the outlet ->
    @IBOutlet weak var editBarButton: UIBarButtonItem!

  2. And here's the button action:

    @IBAction func tappedEditButton(sender: UIBarButtonItem) {

    if tableView.editing {
    tableView.setEditing(false, animated: true)
    editBarButton.title = "Edit"
    } else {
    tableView.setEditing(true, animated: true)
    editBarButton.title = "Done"
    }

Hopefully this works well to solve your problem, best of luck!

How do I set title for UIBarButtonItem?

Use different initialiser that allows you to specify the title:

UIBarButtonItem(title: "title", style: .Plain, target: self, action: "barButtonItemClicked:")

Swift 3.1 Update

UIBarButtonItem(title: "title", style: .plain, target: self, action: #selector(barButtonItemClicked))

Title of a Custom UIBarButtonItem from a UIButton not showing

You should set the title for button of using setTitle:forState: for particular control states, replace your code with below

UIButton *resetButton = [UIButton buttonWithType:UIButtonTypeCustom];
resetButton.frame = CGRectMake(0, 0, 65, 30);
[resetButton addTarget:self action:@selector(speakPhrase:) forControlEvents:UIControlEventTouchUpInside];
resetButton.titleLabel.font = [UIFont fontWithName:@"ProximaNova-Bold" size:19];

[resetButton setTitle:@"RESET" forState:UIControlStateNormal]; //change this line in your code

resetButton.titleLabel.textColor = [UIColor whiteColor];
resetButton.backgroundColor = [UIColor redColor];
resetButton.showsTouchWhenHighlighted = YES;

UIBarButtonItem *resetBarBTN = [[UIBarButtonItem alloc] initWithCustomView:resetButton];

self.navigationItem.rightBarButtonItem = resetBarBTN;

How to set title Edit and Done for Button from UIBarButtonItem?

Consider using UIViewController's editButtonItem property, since it seems to do automatically what you need:

Returns a bar button item that toggles its title and associated state
between Edit and Done.

If you want to keep your approach, you can declare these two buttons as properties and set them up in viewDidLoad for example, like that:

@interface ViewController ()

@property (nonatomic, strong) UIBarButtonItem *editBarButtonItem;
@property (nonatomic, strong) UIBarButtonItem *doneBarButtonItem;

@end

and then

- (void)viewDidLoad
{
[super viewDidLoad];

self.editBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editSelectorName)];
self.doneBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneSelectorName)];
}

and then in your code just do:

self.navigationItem.rightBarButtonItem = self.editBarButtonItem

or

self.navigationItem.rightBarButtonItem = self.doneBarButtonItem


Related Topics



Leave a reply



Submit