UIBarButtonItem更改标题不起作用

时间:2011-05-27 22:08:53

标签: ios uinavigationbar uibarbuttonitem

如何更改UIBarButtonItem的标题?我有以下代码,当我在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;
    }
}

“编辑”按钮正在改变颜色(因此设置样式的行正在工作),但是设置按钮标题的行无效。

8 个答案:

答案 0 :(得分:34)

我已经完成以下操作来动态更改UIBarButtonItem的标题。在这种情况下,我没有使用UIViewTableController,也不能使用标准的editButton。我有一个tableView以及其他子视图的视图,并希望模拟有限的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];
    }
}

请注意,我没有使用UIBarButtonSystemItemEdit barButton ,您无法手动更改该按钮的名称,这是有道理的。

您还可能希望利用possibleTitles属性,以便在更改标题时不会调整按钮的大小。

如果您使用Storyboard / XIB创建/设置这些按钮,请确保将条形按钮项目标识符设置为自定义,以便您控制标题的按钮。

答案 1 :(得分:24)

我遇到了这个问题并通过在初始化时将UIBarButtonItem样式设置为自定义类型来解决它。然后在更改标题值时设置标题。

您可能还想在viewDidLoad方法中设置possibleTitle值,以确保按钮的大小可以正确显示它可能拥有的所有标题。

答案 2 :(得分:8)

如果查看title属性的文档,则明确提到应在将其分配给导航栏之前进行设置。您可以使用两个条形按钮项目 - 一个用于完成,一个用于编辑,而是另外设置它们,而不是正在执行您正在执行的操作。

答案 3 :(得分:8)

在我的情况下,阻止显示标题的是在xib中我选择了Bar按钮项'identifier'属性为'Cancel'。

enter image description here

我甚至在将按钮分配到导航栏之前尝试设置title属性,但标题没有更新。

我这样做了:

enter image description here 它就像我想要的那样开始工作。

答案 4 :(得分:2)

实际上如果您想要在“编辑”和“完成”之间切换,只需使用 self.navigationItem.rightBarButtonItem = self.editButtonItem;

它将为您处理此转换

答案 5 :(得分:1)

每次用户按下按钮时,只需关闭按钮。

- (void)setNavigationButtonAsEdit
{
    UIBarButtonItem* editButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Edit", @"")
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:self
                                                                  action:@selector(editButtonPressed:)];

    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObject:editButton];
}

- (void)setNavigationButtonAsDone
{
    UIBarButtonItem* doneButton = [[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Done", @"")
                                                                   style:UIBarButtonItemStylePlain
                                                                  target:self
                                                                  action:@selector(editButtonPressed:)];
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObject:doneButton];
}

然后创建一个IBAction来处理按钮按下:

- (IBAction)editButtonPressed:(id)sender
{
    NSString* buttonText = [sender title];
    if ([buttonText isEqualToString:NSLocalizedString(@"Edit",@"")])
    {
        [self setNavigationButtonAsDone];
    }
    else
    {
        [self setNavigationButtonAsEdit];
    }
}

答案 6 :(得分:0)

Swift 3.0,3.2或4.0

如果包含tableView对象的自定义ViewController遵循UITableViewController委托和数据源协议,则可以使用以下代码将系统editBarButtonItem附加到navigationItem.leftBarButtonItemnavigationItem.rightBarButtonItem(s):< / p>

func setupTableView() {
    tableView.delegate = self
    tableView.dataSource = self
    navigationItem.rightBarButtonItem = editButtonItem
    editButtonItem.action = #selector(CustomViewController.editTableView(_:))

}

@objc func editTableView(_ sender: UIBarButtonItem) {
    tableView.isEditing = !tableView.isEditing
    if tableView.isEditing {
        sender.title = "Done"
    } else {
        sender.title = "Edit"
    }
}

答案 7 :(得分:-1)

尝试:

UIButton *rightButton;
rightButton = (UIButton*)self.navigationItem.rightBarButtonItem.customView;
[rightButton setTitle:@"Done" forState:UIControlStateNormal];

因为rightBarButtonItem.customView == “the button your added”