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

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

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

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

我该怎么办?

谢谢。


当前回答

这样对我更好。试一试:

 self.navigationController.navigationBar.topItem.backBarButtonItem = [[UIBarButtonItem alloc] 
initWithTitle:@"Back" style:UIBarButtonItemStylePlain target:nil action:nil];

其他回答

我在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

在《ChildVC》中,这对我来说很管用……

self.navigationController.navigationBar.topItem.title = @"Back";

也适用于Swift !

self.navigationController!.navigationBar.topItem!.title = "Back"

斯威夫特4 iOS 11.2 Xcode 9.2

TableViewController1  ---segue--->   TableViewController2

你可以在TableViewController1或TableViewController2中更改返回按钮的文本。

更改TableViewController1内部的返回按钮文本:

1)在viewWillAppear():

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let myBackButton = UIBarButtonItem()
    myBackButton.title = "Custom text"
    navigationItem.backBarButtonItem = myBackButton
}

由于某种原因,viewDidLoad()还太早,不能将后退按钮添加到NavigationItem中。要连接两个tableviewcontroller,在storyboard控件中,从TableViewController1中的TableViewCell拖到TableViewController2的中间,然后在弹出菜单中选择Selection Segue > Show。

2)在tableView(_:didSelectRowAt:):

override func tableView(_ tableView: UITableView, didSelectRowAt: IndexPath) {

    let myButton = UIBarButtonItem()
    myButton.title = "Custom text"
    navigationItem.backBarButtonItem = myButton

    performSegue(withIdentifier: "ShowMyCustomBackButton", sender: nil)
}

要连接两个tableviewcontroller,在storyboard控件中,从TableViewController1上面的黄色小圆圈拖到TableViewController2中间,然后从弹出菜单中选择Manual Segue > Show。然后选择连接两个TableViewControllers的segue,在属性检查器中“Identifier”旁边输入“ShowMyCustomBackButton”。

3)在故事板中:

如果你只是需要静态的自定义文本返回按钮,为TableViewController1选择NavigationItem(它在故事板的目录表中有一个<图标),然后打开属性检查器,在“back button”字段中输入你的自定义文本(确保从该字段中tab出来,更改生效)。


更改TableViewController2内部的返回按钮文本:

1)在viewWillAppear():

class MySecondTableViewController: UITableViewController {

    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let myBackButton = UIBarButtonItem(
            title: "<Custom text",
            style: .plain,
            target: self,
            action: #selector(goBack)  //selector() needs to be paired with an @objc label on the method
        )

        navigationItem.leftBarButtonItem = myBackButton
    }

    @objc func goBack() {
        navigationController?.popViewController(animated: true)
    }

要连接两个tableviewcontroller,在storyboard控件中,从TableViewController1中的TableViewCell拖到TableViewController2的中间,然后在弹出菜单中选择Selection Segue > Show。

迅速:

    // Rename back button
    let backButton = UIBarButtonItem(
        title: "Back",
        style: UIBarButtonItemStyle.Plain, // Note: .Bordered is deprecated
        target: nil,
        action: nil
    )
    self.navigationController!.navigationBar.topItem!.backBarButtonItem = backButton

这在Swift 4和iOS 11中都适用。最常见的情况是,当用户点击表视图中的一行,一个新的视图控制器被推送到导航堆栈时,我需要这样做。通常我希望返回按钮是被点击的行所表示的对象的名称或某些属性。我通常在didSelectRowAt中这样做。是这样的:

override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {

    let object = // get the object this row represents

    let backButton = UIBarButtonItem()
    backButton.title = object.name // or whatever
    self.navigationItem.backBarButtonItem = backButton

    // now show or segue to the next view controller
}