UIBarButtonItem更改标题不起作用

| 如何更改UIBarButtonItem的标题?我有以下代码,当在我的UINavigationBar上按下\“ edit \”按钮时,就会调用该代码。
-(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;
    }
}
\“ edit \”按钮正在更改颜色(因此设置样式的行有效),但是设置按钮标题的行无效。     
已邀请:
        我已完成以下操作来动态更改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,您不能手动更改该按钮的名称,这很有意义。 您可能还想利用maximumTitles属性,以便在更改标题时按钮不会调整大小。 如果您使用情节提要/ XIB创建/设置这些按钮,请确保将要控制其标题的按钮的“条形按钮项标识符”设置为“自定义”。     
        我遇到了这个问题,并通过在初始化时将UIBarButtonItem样式设置为自定义类型来解决。然后,在更改标题值时将设置标题。 您可能还需要在viewDidLoad方法中设置possibleTitle值,以确保针对按钮可能具有的所有可能标题的大小正确设置。     
        在我的情况下,阻止显示标题的原因是在xib中,我选择了Bar按钮项目的“ identifier”属性作为“ Cancel”。 即使在将按钮分配给导航栏之前,我也尝试设置title属性,但是标题没有被更新。 我是这样的: 它开始按我的意愿工作。     
        如果查看
title
属性的文档,则明确提到要在将其分配给导航栏之前进行设置。您可以使用两个条形按钮项目来代替您现在正在做的事情-一个用于完成,一个用于编辑,然后交替设置。     
        实际上,如果您要在\“ Edit \”和\“ Done \”之间切换,只需使用
self.navigationItem.rightBarButtonItem = self.editButtonItem;
它将为您处理此过渡     
        每次用户按下按钮时,只需将其关闭即可。
- (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];
    }
}
    
        Swift 3.0、3.2或4.0 如果包含tableView对象的自定义ViewController遵循UITableViewController Delegate和Datasource协议,则可以使用以下代码将系统editBarButtonItem附加到ѭ6或
navigationItem.rightBarButtonItem
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\"
    }
}
    
        尝试:
UIButton *rightButton;
rightButton = (UIButton*)self.navigationItem.rightBarButtonItem.customView;
[rightButton setTitle:@\"Done\" forState:UIControlStateNormal];
因为ѭ10。     

要回复问题请先登录注册