UIBarButtonItem更改标题不起作用

编程入门 行业动态 更新时间:2024-10-11 17:27:57
本文介绍了UIBarButtonItem更改标题不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如何更改UIBarButtonItem的标题?我在UINavigationBar上按下编辑按钮时会调用以下代码。

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.

推荐答案

我'已完成以下操作以动态更改UIBarButtonItem的标题。在这种情况下,我没有使用UIViewTableController,也不能使用标准的editButton。我有一个tableView以及其他子视图的视图,并希望模拟有限的UIViewTableController的行为。

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

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

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

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

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

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

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.

更多推荐

UIBarButtonItem更改标题不起作用

本文发布于:2023-11-26 20:29:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635084.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:不起作用   标题   UIBarButtonItem

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!