ios UIBarButtonItem 更改标题不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6158169/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
UIBarButtonItem changing title not working
提问by CodeGuy
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.
如何更改 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;
}
}
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.
“编辑”按钮正在改变颜色(因此设置样式的行正在工作),但是设置按钮标题的行不起作用。
回答by Seamus
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.
我已完成以下操作以动态更改 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];
}
}
Note that I didn't use the the UIBarButtonSystemItemEdit barButton, you cannot manually change the name of that button, which makes sense.
请注意,我没有使用 UIBarButtonSystemItemEdit barButton,您无法手动更改该按钮的名称,这是有道理的。
You also might want to take advantage of the possibleTitles property so that the button doesn't resize when you change the title.
您可能还想利用 possibleTitles 属性,以便在更改标题时不会调整按钮的大小。
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.
如果您使用 Storyboard/XIB 来创建/设置这些按钮,请确保将 Bar Button Item Identifier 设置为您想要控制标题的按钮的 Custom。
回答by Mark Bridges
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.
我遇到了这个问题,并通过在初始化时将 UIBarButtonItem 样式设置为自定义类型来解决它。然后在更改标题值时设置标题。
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.
您可能还想在 viewDidLoad 方法中设置 possibleTitle 值,以确保按钮的大小适合所有可能的标题。
回答by Deepak Danduprolu
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.
如果您查看该title
属性的文档,会明确提到您应该在将其分配给导航栏之前对其进行设置。您可以使用两个条形按钮项目,一个用于完成,一个用于编辑,而不是立即执行您正在执行的操作,并交替设置它们。
回答by Joe M
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'.
在我的情况下,阻止显示标题的原因是在 xib 中,我将栏按钮项“标识符”属性选择为“取消”。
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.
它开始按照我的意愿工作。
回答by gyan
Actually if all you want if switching between "Edit" and "Done", just use
self.navigationItem.rightBarButtonItem = self.editButtonItem;
实际上,如果您想要在“编辑”和“完成”之间切换,只需使用
self.navigationItem.rightBarButtonItem = self.editButtonItem;
It will handle this transition for you
它将为您处理此过渡
回答by Scott Allen
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 来处理按钮按下:
- (IBAction)editButtonPressed:(id)sender
{
NSString* buttonText = [sender title];
if ([buttonText isEqualToString:NSLocalizedString(@"Edit",@"")])
{
[self setNavigationButtonAsDone];
}
else
{
[self setNavigationButtonAsEdit];
}
}
回答by umarqattan
Swift 3.0, 3.2, or 4.0
Swift 3.0、3.2 或 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:
如果包含 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"
}
}
回答by dadoftutu
Try:
尝试:
UIButton *rightButton;
rightButton = (UIButton*)self.navigationItem.rightBarButtonItem.customView;
[rightButton setTitle:@"Done" forState:UIControlStateNormal];
Because rightBarButtonItem.customView == “the button your added”
.
因为rightBarButtonItem.customView == “the button your added”
。