Take the 2-minute tour ×

I'm trying to change the title of UIBarButtonItem when button is clicked. Here's the code that I have... but it doesn't work.

UIBarButtonItem * btnleft1 = [[UIBarButtonItem alloc] initWithTitle:@"Test2" 

self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:btn, btnleft, btnleft1, nil];

- (void)TestingThis:(id)sender {
    if (self.navigationItem.rightBarButtonItem.title == @"Test2") {
        self.navigationItem.rightBarButtonItem.title = @"Done";
        self.navigationItem.rightBarButtonItem.title = @"Test2";

So that's the code that I'm using. Please keep in mind that button "btnleft1" is in rightBarButtonItem"S" and not in rightBarButtonItem. If i change it to rightbarbuttonitems.title... it gives me an error.

share|improve this question
add comment

2 Answers

Late answer but for future reference, you can use custom button to do this :

      CGRect frame = CGRectMake(10, 6, 60, 30);
      UIButton *customButton = [[UIButton alloc] initWithFrame:frame];
      [customButton setTitle:@"Done" forState:UIControlStateNormal];
      [customButton setTitle:@"Undone" forState:UIControlStateSelected];
      [customButton addTarget:self action:@selector(selectDoneClicked:) forControlEvents:UIControlEventTouchUpInside];
      UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithCustomView:customButton ];
      [self.navigationItem setLeftBarButtonItem:leftButton];
share|improve this answer
add comment

Try setting the title explicitly. You can do this with code:[(UIBarButtonItem *)item setTitle:@"someText"];
Here's a helpful link: Change the text of a UILabel (UIBarButtonItem) on a toolbar programmatically
Hope this helped!

share|improve this answer
Even if I put this on my (void) TestingThis [(UIBarButtonItem *)btnleft1 setTitle:@"Test5"]; It wont rename it. Basically I want to be able to press btnleft1 and change its title from Test2 to Done and vice versa –  MichaelD Jun 15 '12 at 22:24
add comment

Your Answer


