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;
}
}
编辑按钮正在更改颜色(因此设置样式的行正在工作),但是设置按钮标题的行不起作用。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
尝试:
因为
rightBarButtonItem.customView == “您添加的按钮”
。Try:
Because
rightBarButtonItem.customView == “the button your added”
.我已完成以下操作来动态更改 UIBarButtonItem 的标题。在这种情况下,我没有使用 UIViewTableController 并且无法使用标准的 editButton。我有一个带有 tableView 以及其他子视图的视图,并且想要模拟有限的 UIViewTableController 的行为。
请注意,我没有使用 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.
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.
我遇到了这个问题,并通过在初始化时将 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.
就我而言,阻止显示标题的原因是在 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'.
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:
And it started working just as I wanted.
如果您查看
title
属性,明确提到您应该在将其分配给导航栏之前设置它。您可以使用两个栏按钮项,一个用于完成,一个用于编辑,然后交替设置它们,而不是执行您现在正在执行的操作。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.实际上,如果您想要在“编辑”和“完成”之间切换,只需使用
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
Swift 3.0、3.2 或 4.0
如果包含 tableView 对象的自定义 ViewController 遵循 UITableViewController Delegate 和 Datasource 协议,则可以将系统 editBarButtonItem 附加到您的
navigationItem.leftBarButtonItem
(s) 或navigationItem。 rightBarButtonItem
(s) 具有以下代码: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) ornavigationItem.rightBarButtonItem
(s) with the following code:只需在用户每次按下按钮时关闭按钮即可。
然后创建一个 IBAction 来处理按钮按下:
Just switch out the buttons each time the user presses them.
And then create an IBAction to handle the button press: