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

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

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

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

我该怎么办?

谢谢。


当前回答

我发现,更改返回按钮名称最简单的方法是将视图控制器的标题设置为返回按钮的标题,然后将视图控制器导航项中的titleView替换为带有它的真实名称的自定义标签。

是这样的:

CustomViewController.m

@implementation CustomViewController

- (NSString*)title {
    return @"Back Button Title";
}

- (void)viewDidLoad {
    [super viewDidLoad];
    UILabel* customTitleView = [[UILabel alloc] initWithFrame:CGRectZero];
    customTitleView.text = @"Navigation Bar 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];
}

@end

这将使你的标题在UINavigationBar看起来就像它是本地的。让视图控制器能够拥有分离的标题和返回按钮标题。

在视图控制器A和B的情况下,A负责告诉它的后退按钮应该是什么样子,而B是显示的。

编辑:这也保持了后退按钮的原始外观(左边带箭头的栏按钮项)。

其他回答

在《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。

iOS 11+解决方案-不需要再次创建后退按钮。

将backButtonTitle设置为前一屏幕上的一个空格。

// If navigation is from A -> B, set in A's `viewDidLoad`.
navigationItem.backButtonTitle = " "

在Xcode 4.5中使用storyboard,到目前为止,当Back按钮的值不需要动态改变时,我发现最简单的解决方案是使用“Back button”字段与视图控制器的导航项相关联,你想让“Back”按钮表示其他内容。

例如,在下面的截图中,我想要视图控制器的后退按钮(s),我推有“后退”作为后退按钮的标题。

当然,如果你每次都需要返回按钮显示稍微不同的内容,这就行不通了……这里有所有其他的解决方案。

好的。我个人讨厌所有这些选择。因此我想出了我自己的。

根据我所看到的信息。看起来Previous视图控制器控制着它自己的“Back”按钮,该按钮将显示在被推的视图控制器上。

我已经为控制器上想要更改Back Button的navigationItem创建了一个Lazy Load方法。

我的是一个邀请买家控制器

邀请买家是默认设置的文本。

但后退按钮需要是“邀请”

下面是我用来创建后退按钮的代码。

我把这段代码放在Controller's Implementatio (.m)文件的顶部,它会自动覆盖super的方法。

- (UINavigationItem *)navigationItem{
    UINavigationItem *item = [super navigationItem];
    if (item != nil && item.backBarButtonItem == nil)
    {
        item.backBarButtonItem = [[[UIBarButtonItem alloc] init] autorelease];
        item.backBarButtonItem.title = @"Invite";
    }

    return item;
}

我觉得这是一种更优雅的方式。

我把这些代码放在一个地方,在需要时自动填充。

不需要在每个推送请求之前调用代码。

希望这能有所帮助