Ios UIBarButtonItem更改标题不起作用

Ios UIBarButtonItem更改标题不起作用,ios,objective-c,uinavigationbar,uibarbuttonitem,Ios,Objective C,Uinavigationbar,Uibarbuttonitem,如何更改UIBarButtonItem的标题?当按下我的UINavigationBar上的编辑按钮时,会调用以下代码 -(void)editButtonSelected:(id)sender { NSLog(@"edit button selected!"); if(editing) { NSLog(@"notediting"); [super setEditing:NO animated:NO];

如何更改
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;
    }
}

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

如果查看属性的文档,会明确指出,在将其指定给导航栏之前,应先对其进行设置。您可以使用两个条形按钮项(一个用于完成,另一个用于编辑)来替代现在正在执行的操作,并交替设置它们。

我已经完成了以下操作来动态更改UIBarButtonim的标题。在这种情况下,我没有使用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];
    }
}
请注意,我没有使用UIBarButtonSystemiteEdit按钮,您无法手动更改该按钮的名称,这是有意义的

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


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

我遇到了这个问题,并在初始化时将UIBarButtonim样式设置为自定义类型,从而解决了这个问题。然后在更改标题值时设置标题


您可能还希望在viewDidLoad方法中设置PossibleTile值,以确保按钮的大小适合其可能拥有的所有标题。

只要在用户每次按下按钮时将其关闭即可

- (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];
}
然后创建一个iAction来处理按钮按下:

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


因为
rightbarbuttoneim.customView==“您添加的按钮”

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


它将为您处理此转换

在我的情况下,阻止显示标题的原因是在xib中,我选择了条形按钮项“标识符”属性作为“取消”

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

我是这样做的:

它开始按照我的要求工作。

Swift 3.0、3.2或4.0 如果包含tableView对象的自定义ViewController遵循UITableViewController委托和数据源协议,则可以使用以下代码将系统编辑器BarButtonim附加到
导航项。LeftBarButtonim
(s)或
导航项。RightBarButtonim
(s):

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

确切地e、 g:只需编码:UIBarButtonItem*doneButton=[[UIBarButtonItem alloc]initWithTitle:@“完成”样式:UIBarButtonItemStyleDone目标:自我操作:@selector(someMethod)];创建一个“完成”按钮。是的,它确实说您应该在添加之前设置标题,但是PossibleTiles属性的含义是什么?我以编程方式添加了条形按钮,并设置了
PossibleTiles
属性。正如Gordon所说,
possibleTiles
属性如果不能更改标题,那么
possibleTiles
属性的意义是什么?
possibleTiles
是在导航栏上有更多或更少的空间时提供替代选项(例如,横向与纵向,iPhone 4与iPhone 6 Plus与iPad)。我认为调用barButton.PossibleTiles是多余的。我发现,只要在editMode方法上设置调用方的类型,并使用sender.title=@“某物”谢谢@Seamus!我的诀窍是不使用“编辑”即可通常在UITableViewController中工作的按钮。我相信,因为它是本机的“编辑”按钮,所以不允许我更改标题。我将我的设置为“自定义”.1到
possibleTiles
。奇怪的是,你必须事先声明可能性,但这似乎为我解决了这个问题。
如果你使用故事板/XIB创建/设置这些按钮,请确保为你想要控制其标题的按钮将条形按钮项目标识符设置为自定义。
这就是我所做的正在查找。谢谢!这将切换一个按钮,而不会像问题所问的那样更改标题。我不知道苹果如何实现这个editButtonItem,但它的行为就像一个按钮在“编辑”和“完成”之间切换。如果你想将标题更改为任何其他内容,这将不起作用。但从发布的代码OP来看,这正是他所需要的。如果他使用的是
uibarbuttonsystemedit
,那么是的,苹果使用了大量不可变的对象,因此无法将标题从“编辑”更改为“完成”是有意义的在一个包含“编辑”一词的类上。超级简单的解决方案——效果很好。我使用的是故事板,所以即使有一个多用途的编辑按钮,您也可以使用-(BOOL)shouldPerformSegueWithIdentifier:截获用户的触摸,并执行自定义操作。非常感谢这个简单的解决方案(这应该是显而易见的)解决方案!是的,自定义类型将允许您访问标题。这是真的,谢谢!“您添加的按钮”?这是什么意思?
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"
    }
}