1

单击按钮时,我正在尝试更改 UIBarButtonItem 的标题。这是我拥有的代码......但它不起作用。

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";
    }
}

这就是我正在使用的代码。请记住,按钮“btnleft1”在 rightBarButtonItem“S”中,而不是在 rightBarButtonItem 中。如果我将其更改为 rightbarbuttonitems.title... 它会给我一个错误。

4

2 回答 2

1

迟到的答案,但为了将来参考,您可以使用自定义按钮来执行此操作:

      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];
于 2013-05-30T16:14:45.030 回答
0

尝试明确设置标题。您可以使用代码执行此操作:[(UIBarButtonItem *)item setTitle:@"someText"];
这是一个有用的链接:以编程方式更改工具栏上的 UILabel (UIBarButtonItem) 的文本
希望这有帮助!

于 2012-06-15T20:27:30.373 回答