UIBarButtonItem更改标题不起作用
我如何更改UIBarButtonItem的标题? 我有下面的代码,当我的UINavigationBar上按下“编辑”button被调用。
-(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; } }
“编辑”button正在改变颜色(所以设置样式的行正在工作),但是设置button标题的行不起作用。
我做了以下dynamic更改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 ,您不能手动更改该button的名称,这是有道理的。
您也可能要利用possibleTitles属性,以便在更改标题时不会调整button的大小。
如果您正在使用Storyboard / XIB来创build/设置这些button,请确保将button项目标识符设置为自定义,以便控制标题的button。
我有这个问题,并通过设置UIBarButtonItem样式初始化时自定义types解决它。 然后标题会在更改标题值时设置。
您可能还想在viewDidLoad方法中设置possibleTitle值,以确保该button的大小可以正确设置,以适应所有可能的标题。
如果您查看title
属性的文档,明确提到您应该在将其分配给导航栏之前进行设置。 您现在可以不使用自己正在做的事情,而是使用两个条形button项目 – 一个用于完成,另一个用于编辑,并将它们交替设置。
在我的情况是什么阻止显示的标题是在xib我select了“button”项目的“标识符”属性为“取消”。
我甚至在将button分配给导航栏之前尝试设置title属性,但标题没有被更新。
我是这样做的:
它开始工作,就像我想要的。
每次用户按下button时都要closuresbutton。
- (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]; }
然后创build一个IBAction来处理button按下:
- (IBAction)editButtonPressed:(id)sender { NSString* buttonText = [sender title]; if ([buttonText isEqualToString:NSLocalizedString(@"Edit",@"")]) { [self setNavigationButtonAsDone]; } else { [self setNavigationButtonAsEdit]; } }
其实如果你想要的只是切换“编辑”和“完成”,只需使用self.navigationItem.rightBarButtonItem = self.editButtonItem;
它会为你处理这个过渡
Swift 3.0,3.2或4.0
如果包含tableView对象的自定义ViewController遵循UITableViewController委托和数据源协议,则可以使用以下代码将系统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" } }
尝试:
UIButton *rightButton; rightButton = (UIButton*)self.navigationItem.rightBarButtonItem.customView; [rightButton setTitle:@"Done" forState:UIControlStateNormal];
因为rightBarButtonItem.customView == “the button your added”
。
- 是否有可能停用spriteKit物理机构中的碰撞?
- 如何#在ios中将macros定义为macros
- 将SCNNode的枢轴设置为限制体积中心而不更改节点的位置
- 用块replace委托方法的最佳技术
- 终止应用程序,由于未捕获的exception'NSInvalidArgumentException' – ios谷歌login
- AVFoundation相机预览图层不起作用
- 无法从iOS 7的App Store收据中获取original_application_version
- UIWebView自动旋转损坏网页视图内容(截图里面)
- NSLayoutManager:调用setLocation(_:forStartOfGlyphRange :)将禁用整个string中的字距?