UIBarButtonItem changing title not working

36,671

Solution 1

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.

Solution 2

I had this problem and resolved it by setting the UIBarButtonItem style to the custom type when it's initialised. Then the titles would set when changing their title values.

You may also want to set the possibleTitle value in the viewDidLoad method to ensure the button is sized correctly for all the possible titles it can have.

Solution 3

If you look at the documentation of the title property, it is explicitly mentioned that you should set it before assigning it to the navigation bar. Instead of doing what you're doing right now, you can use two bar button items – one for done and one for edit, and set them alternatively.

Solution 4

In my case what prevented the title being displayed was that in the xib I'd selected the Bar button item 'identifier' property as 'Cancel'.

enter image description here

I tried setting the title property even before assigning the button to the navigation bar, but the title was not being updated.

I made it like this:

enter image description here And it started working just as I wanted.

Solution 5

Actually if all you want if switching between "Edit" and "Done", just use self.navigationItem.rightBarButtonItem = self.editButtonItem;

It will handle this transition for you

Share:
36,671

Related videos on Youtube

CodeGuy
Author by

CodeGuy

Updated on March 23, 2021

Comments

  • CodeGuy
    CodeGuy about 3 years

    How can I change the title of a UIBarButtonItem? I have the following code which is called when an edit button is pressed on my UINavigationBar.

    -(void)editButtonSelected:(id)sender {
        NSLog(@"edit button selected!");
        if(editing) {
            NSLog(@"notediting");
            [super setEditing:NO animated:NO];
            [tableView setEditing:NO animated:NO];
            [tableView reloadData];
            [rightButtonItem setTitle:@"Edit"];
            [rightButtonItem setStyle:UIBarButtonItemStylePlain];
            editing = false;
        }
        else {
            NSLog(@"editing");
            [super setEditing:YES animated:YES];
            [tableView setEditing:YES animated:YES];
            [tableView reloadData];
            [rightButtonItem setTitle:@"Done"];
            [rightButtonItem setStyle:UIBarButtonItemStyleDone];
            editing = true;
        }
    }
    

    The edit button is changing color (so the line which sets the style is working), however the line which sets the title of the button is not working.

  • Oscar Salguero
    Oscar Salguero about 12 years
    Exactly! e.g.: It is enough to code: UIBarButtonItem *doneButton = [[UIBarButtonItem alloc] initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(someMethod)]; to create a Done button.
  • Gordon Dove
    Gordon Dove over 11 years
    Yes, it does say you should set the title before adding, but what then is the sense of the possibleTitles property?
  • Simon Curd
    Simon Curd over 10 years
    I think the call to barButton.possibleTitles is redundant. I found this worked just by setting the type of the caller on the editMode method and using sender.title = @"something"
  • wcochran
    wcochran almost 10 years
    I added the bar button programmatically and set the possibleTitles property. I had no problem changing the title property on the fly -- as Gordon says -- what the point of the possibleTitles property if the title can't be changed?
  • Steve Buzonas
    Steve Buzonas over 9 years
    This would switch a button and not change the title as the question asks.
  • gyan
    gyan over 9 years
    I don't know how apple implements this editButtonItem, but it does behave like one single button switching between "Edit" and "Done". If you want to change the title to anything else, this will not work. But from the code OP posted, this is exactly what he needs.
  • Steve Buzonas
    Steve Buzonas over 9 years
    If he's using UIBarButtonSystemItemEdit then yes apple utilizes a lot of immutable objects, and it makes sense to not be able to change the title from "Edit" to "Done" on a class with the word edit in it.
  • kbpontius
    kbpontius almost 9 years
    Thanks @Seamus! The trick for me was to NOT use the "Edit" button that would normally work in a UITableViewController. I believe that because it was the native "Edit" button it wasn't letting me change the title. I set mine to "Custom".
  • jrc
    jrc over 8 years
    possibleTitles is to provide alternatives for when there is more or less space on the navigation bar (e.g. landscape vs portrait, iPhone 4 vs iPhone 6 Plus vs iPad).
  • Mike Critchley
    Mike Critchley over 8 years
    Super simple solution -- works fine. I'm using storyboards, so even with a multipurpose edit button, you can intercept user touches with - (BOOL) shouldPerformSegueWithIdentifier: and doing custom actions. Thanks a million for this simple (and what should have been obvious) solution!
  • Edward Ashak
    Edward Ashak about 8 years
    yea, Custom type will let you access the title
  • daniel kilinskas
    daniel kilinskas about 8 years
    That is true, Thanks !
  • mylogon
    mylogon about 7 years
    +1 to the possibleTitles. Weird that you have to declare the possibilities up front, but this seemed to resolve this for me.
  • Alex Zavatone
    Alex Zavatone over 6 years
    "The button your added"? What does that mean?
  • Phontaine Judd
    Phontaine Judd over 4 years
    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. That's what I was looking for. Thanks!