UIBarButtonItem 更改标题不起作用

发布于 2024-11-10 04:38:02 字数 895 浏览 5 评论 0原文

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

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

How can I change the title of a UIBarButtonItem? I have the following code which is called when an edit button is pressed on my 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;
    }
}

The edit button is changing color (so the line which sets the style is working), however the line which sets the title of the button is not working.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

魔法少女 2024-11-17 04:38:03

尝试:

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

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

Try:

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

Because rightBarButtonItem.customView == “the button your added”.

凉宸 2024-11-17 04:38:02

我已完成以下操作来动态更改 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 来创建/设置这些按钮,请确保将您想要控制其标题的按钮的栏按钮项目标识符设置为自定义。

I've done the following to dynamically change the title of a UIBarButtonItem. In this situation I am not using a UIViewTableController and cannot use the standard editButton. I have a view with a tableView as well as other subviews and wanted to emulate the behavior of the limited 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];
    }
}

Note that I didn't use the the UIBarButtonSystemItemEdit barButton, you cannot manually change the name of that button, which makes sense.

You also might want to take advantage of the possibleTitles property so that the button doesn't resize when you change the title.

If you are using a Storyboard/XIB to create/set these buttons, ensure that the Bar Button Item Identifier is set to Custom for the button which you'd want to control the title for.

平安喜乐 2024-11-17 04:38:02

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

您可能还需要在 viewDidLoad 方法中设置 possibleTitle 值,以确保按钮的大小适合其可能具有的所有可能标题。

I had this problem and resolved it by setting the UIBarButtonItem style to the custom type when it's initialised. Then the titles would set when changing their title values.

You may also want to set the possibleTitle value in the viewDidLoad method to ensure the button is sized correctly for all the possible titles it can have.

神经大条 2024-11-17 04:38:02

就我而言,阻止显示标题的原因是在 xib 中我选择了 Bar 按钮项“identifier”属性作为“Cancel”。

在此处输入图像描述

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

我这样做是这样的:

在此处输入图像描述
它开始按照我想要的方式工作。

In my case what prevented the title being displayed was that in the xib I'd selected the Bar button item 'identifier' property as 'Cancel'.

enter image description here

I tried setting the title property even before assigning the button to the navigation bar, but the title was not being updated.

I made it like this:

enter image description here
And it started working just as I wanted.

独自←快乐 2024-11-17 04:38:02

If you look at the documentation of the title property, it is explicitly mentioned that you should set it before assigning it to the navigation bar. Instead of doing what you're doing right now, you can use two bar button items – one for done and one for edit, and set them alternatively.

梦旅人picnic 2024-11-17 04:38:02

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

它将为您处理此转换

Actually if all you want if switching between "Edit" and "Done", just use
self.navigationItem.rightBarButtonItem = self.editButtonItem;

It will handle this transition for you

谁与争疯 2024-11-17 04:38:02

Swift 3.0、3.2 或 4.0

如果包含 tableView 对象的自定义 ViewController 遵循 UITableViewController Delegate 和 Datasource 协议,则可以将系统 editBarButtonItem 附加到您的 navigationItem.leftBarButtonItem(s) 或 navigationItem。 rightBarButtonItem(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"
    }
}

Swift 3.0, 3.2, or 4.0

If your custom ViewController containing a tableView object follows the UITableViewController Delegate and Datasource protocols, you can append a system editBarButtonItem to your navigationItem.leftBarButtonItem(s) or navigationItem.rightBarButtonItem(s) with the following code:

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"
    }
}
看春风乍起 2024-11-17 04:38:02

只需在用户每次按下按钮时关闭按钮即可。

- (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];
    }
}

Just switch out the buttons each time the user presses them.

- (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];
}

And then create an IBAction to handle the button press:

- (IBAction)editButtonPressed:(id)sender
{
    NSString* buttonText = [sender title];
    if ([buttonText isEqualToString:NSLocalizedString(@"Edit",@"")])
    {
        [self setNavigationButtonAsDone];
    }
    else
    {
        [self setNavigationButtonAsEdit];
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文