当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。

我想把按钮上显示的文本更改为其他内容。

我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。

self.navigationItem.leftBarButtonItem.title = @"Log Out";

我该怎么办?

谢谢。


当前回答

对于那些使用故事板的人,只需选择父视图(不是持有目标视图的那个)视图控制器框架(确保你在导航栏上单击右键,然后打开属性检查器,在那里你会发现三个表单输入。第三个“后退按钮”是我们正在寻找的。

其他回答

使用下面的代码行:

UIBarButtonItem *newBackButton =
[[UIBarButtonItem alloc] initWithTitle:@"hello"
                                 style:UIBarButtonItemStylePlain
                                target:nil
                                action:nil];

self.navigationItem.leftBarButtonItems =[[NSArray alloc] initWithObjects:newBackButton, nil];

self.navigationItem.leftItemsSupplementBackButton = YES;

我在iOS新,但我会提供我的覆盖导航控制器类的非常简单的答案。 我有简单的覆盖推和弹出方法,并保存以前的视图控制器的标题。抱歉在js block中进行了粘贴。有点困惑如何过去它在正常的代码块。

#import "MyCustomNavController.h" @implementation MyCustomNavController { NSString *_savedTitle; } - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated withBackBtnTitle:(NSString *)title { _savedTitle = self.topViewController.title; self.topViewController.title = title; [super pushViewController:viewController animated:animated]; } - (UIViewController *)popViewControllerAnimated:(BOOL)animated { [self.viewControllers objectAtIndex:self.viewControllers.count - 2].title = _savedTitle; return [super popViewControllerAnimated:animated]; } @end

斯威夫特版本:

在你的子ViewController中:

override func viewWillAppear(animated: Bool) {
    super.viewWillAppear(animated)
    self.navigationController?.navigationBar.backItem?.title = "TEXT"
}

self.navigationController.navigationBar.backItem.title = @"TEXT";

在Swift中:

self.navigationController?.navigationBar.backItem?.title = "TEXT"

我们有两个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)