我有一个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?如何解决?
答案 0 :(得分:1)
你必须实际设置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];
}