当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前回答
这是另一种方法。
在你的父视图控制器中,实现以下方法:
- (void) setBackBarButtonItemTitle:(NSString *)newTitle {
self.navigationItem.backBarButtonItem.title = newTitle;
}
在你的子视图控制器中,当你想要改变标题时,这将会工作:
NSArray *viewControllerArray = [self.navigationController viewControllers];
int parentViewControllerIndex = [viewControllerArray count] - 2;
[[viewControllerArray objectAtIndex:parentViewControllerIndex] setBackBarButtonItemTitle:@"New Title"];
我从来没有能够让parentViewController属性工作:
[(ParentViewController *)(self.navigationController.parentViewController) setBackBarButtonItemTitle:@"New Title"];
我不知道这是一个错误还是我没有正确使用它。但是在viewControllers数组中抓取倒数第二个视图控制器指向父视图控制器,我可以用那个引用正确地调用父方法。
其他回答
在《ChildVC》中,这对我来说很管用……
self.navigationController.navigationBar.topItem.title = @"Back";
也适用于Swift !
self.navigationController!.navigationBar.topItem!.title = "Back"
问题:导航栏中的“返回”文本不能被替换。
原因:在推送视图后,导航栏中设置了“Back”标签,因为父视图控制器中的.title属性被设置为nil(或未初始化)。
一个解决方案:如果你设置self.title="Whatever…",你会看到在按下新的视图控制器后,"Back"将出现"Whatever…"。
self.navigationItem.backBarButtonItem = [[[UIBarButtonItem alloc]
initWithTitle:@"Log out"
style:UIBarButtonItemStyleDone
target:nil
action:nil] autorelease];
你可以把它放在父控制器代码中的任何你喜欢的地方,这允许你为不同的子视图有不同的后退按钮。
Most of solutions kills the original style of BackButton (The left arrowed bar button) while adding a usual button with desired title. So to keep the original style there are 2 ways: 1st: To use undocumented button style (110 or something like that) which I prefer not to do. But if you want you could find how to do it here, on stackoverflow. 2nd: To use I the Trenskow's idea. I liked it and I use it a bit changed. Instead of overriding - (NSString*)title I've decided to keep the original title in the following way (which allows me to use nib's titles as well as given title at push state btw).
- (void)viewDidLoad {
[super viewDidLoad];
static NSString * backButtonTitle=@"Back"; //or whatever u want
if (![self.title isEqualToString:backButtonTitle]){
UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
customTitleView.text = self.title; // original title
customTitleView.font = [UIFont boldSystemFontOfSize:20];
customTitleView.backgroundColor = [UIColor clearColor];
customTitleView.textColor = [UIColor whiteColor];
customTitleView.shadowColor = [UIColor colorWithRed:0.0 green:0.0 blue:0.0 alpha:0.5];
customTitleView.shadowOffset = CGSizeMake(0, -1);
[customTitleView sizeToFit];
self.navigationItem.titleView = [customTitleView autorelease];
self.title = backButtonTitle;
}
}
这个解决方案效果很好,看起来很自然。此外,如果在viewDidLoad方法中使用它,它可以防止执行超过1次。 我也试过杰塞德克的解决方案,但看起来很糟糕。它导致可见的用户标题栏的变化,从原来的BackButton的期望和回来。
我们有两个VC A和B。
如果你想在B中更改标题,请在A中编写这段代码
- (IBAction)goToBViewController:(UIButton *)sender {
BViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"VC"];
UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle:@"Your title here"
style:UIBarButtonItemStylePlain
target:nil
action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[self.navigationController pushViewController:vc animated:NO];
}
Swift 4.1 Xcode 9.4
let secondViewController = self.storyboard?.instantiateViewController(withIdentifier: "VC"])
let newBackButton = UIBarButtonItem.init(title: "Your title here", style: UIBarButtonItemStyle.plain, target: nil, action: nil)
navigationController?.navigationBar.topItem?.backBarButtonItem = newBackButton
navigationController?.pushViewController(secondViewController!, animated: true)