【问题标题】:How to change Title of UIBarButtonItem when button is clicked - iPhone单击按钮时如何更改 UIBarButtonItem 的标题 - iPhone
【发布时间】:2012-06-15 19:54:20
【问题描述】:

我正在尝试在单击按钮时更改 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... 它会给我一个错误。

【问题讨论】:

    标签: ios ios5 uibarbuttonitem navigationitem


    【解决方案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];
    

    【讨论】:

      【解决方案2】:

      尝试明确设置标题。 你可以用代码做到这一点:[(UIBarButtonItem *)item setTitle:@"someText"];
      这是一个有用的链接:Change the text of a UILabel (UIBarButtonItem) on a toolbar programmatically
      希望这有帮助!

      【讨论】:

      • 即使我把它放在我的 (void) TestingThis [(UIBarButtonItem *)btnleft1 setTitle:@"Test5"];它不会重命名它。基本上我希望能够按 btnleft1 并将其标题从 Test2 更改为 Done,反之亦然
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-16
      • 1970-01-01
      • 1970-01-01
      • 2019-07-06
      • 1970-01-01
      相关资源
      最近更新 更多