UIBarButtonItem更改标题不起作用

Cod*_*Guy 24 uinavigationbar uibarbuttonitem ios

如何更改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;
    }
}
Run Code Online (Sandbox Code Playgroud)

"编辑"按钮正在改变颜色(因此设置样式的行正在工作),但是设置按钮标题的行不起作用.

Sea*_*mus 35

我已经完成了以下操作来动态更改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];
    }
}
Run Code Online (Sandbox Code Playgroud)

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

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

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

  • 谢谢@Seamus!我的诀窍是不要使用通常在UITableViewController中工作的"编辑"按钮.我相信因为它是原生的"编辑"按钮,所以不让我改变标题.我将我的设置为"自定义". (2认同)

Mar*_*ges 24

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

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


Dee*_*olu 8

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

  • 是的,它确实说你应该在添加之前设置标题,但是对于possibleTitles属性的意义是什么呢? (7认同)

Joe*_*e M 8

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

在此输入图像描述

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

我这样做了:

在此输入图像描述 它就像我想要的那样开始工作.