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创建/设置这些按钮,请确保将条形按钮项标识符设置为自定义,以便为您要控制标题的按钮设置.
Mar*_*ges 24
我有这个问题并通过在初始化时将UIBarButtonItem样式设置为自定义类型来解决它.然后在更改标题值时设置标题.
您可能还希望在viewDidLoad方法中设置possibleTitle值,以确保按钮的大小适合其可能拥有的所有可能标题.
在我的情况下,阻止显示标题的是在xib中我选择了Bar按钮项'identifier'属性为'Cancel'.
我甚至在将按钮分配到导航栏之前尝试设置title属性,但标题没有更新.
我这样做了:
它就像我想要的那样开始工作.