1

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" 
                                                      style:UIBarButtonItemStyleDone 
                                                      target:self
                                                      action:@selector(TestingThis:)];

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

- (void)TestingThis:(id)sender {
    if (self.navigationItem.rightBarButtonItem.title == @"Test2") {
        self.navigationItem.rightBarButtonItem.title = @"Done";
    }
    else
    {
        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.

MichaelD
  • 31
  • 1
  • 6

2 Answers2

1

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];
Zeev Vax
  • 914
  • 7
  • 13
0

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!

Community
  • 1
  • 1
waylonion
  • 6,866
  • 8
  • 51
  • 92
  • 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