我有一个UIViewController,并在InterfaceBuilder中添加了一个NavigationItem来查看标题,我发现我无法将backBarButtonItem拖动到navigationItem上,只能添加leftBarButtonItem和rightBarButtonItem。因此,我尝试在控制器代码中将backBarButtonItem添加到navigationItem元素:
- (void)viewWillAppear:(BOOL)animated {
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
self.myNavigationItem.backBarButtonItem = _backButton;
[_backButton release];
}
但是视图上仍然没有backBarButtonItem吗?如何解决?
最佳答案
您实际上必须设置leftBarButtonItem,而不是backBarButtonItem,只有在设置self.hidesBackBarButtonItem之后才这样设置:
- (void)viewWillAppear:(BOOL)animated {
UIBarButtonItem *_backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back" style:UIBarButtonItemStyleDone target:nil action:nil];
self.navigationItem.hidesBackButtonItem = YES;
self.myNavigationItem.leftBarButtonItem = _backButton;
[_backButton release];
}