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

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

我试着把下面这行代码放在视图控制器的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是显示的。

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

其他回答

我发现最好是在推到下一个视图控制器之前,在导航堆栈中将当前视图控制器的标题更改为后退按钮的所需文本。

例如

self.navigationItem.title = @"Desired back button text";
[self.navigationController pushViewController:QAVC animated:NO];

然后在viewDidAppear中将标题设置回原始VC所需的标题。瞧!

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

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

也适用于Swift !

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

这应该放在调用名为NewTitle的ViewController的方法中。 就在push或popViewController语句之前。

UIBarButtonItem *newBackButton = 
        [[UIBarButtonItem alloc] initWithTitle:@"NewTitle" 
                                         style:UIBarButtonItemStyleBordered 
                                        target:nil 
                                        action:nil];
[[self navigationItem] setBackBarButtonItem:newBackButton];
[newBackButton release];

根据UINavigationBar>backItem的文档

如果最上面导航项的leftBarButtonItem属性为 Nil,导航栏显示一个返回按钮,该按钮的标题是派生的 属性中的项。

但是设置backItem。backBarButtonItem在第一次viewWillAppear时不起作用。设置topItem。backBarButtonItem只适用于第一次viewWillAppear。因为navigationBar。topItem仍然指向previousViewController.navigationItem。在viewWillLayoutSubviews中,topItem和backItem被更新。在第一次viewWillAppear之后,我们应该设置backitem。backbarbuttonitem。

答:设置一个backBarButtonItem到前一个viewController的navigationItem,无论何时何地在你的当前viewController(顶部viewController)。你可以在viewWillAppear或viewDidLoad中使用这段代码。查看我的博客文章iOS设置导航栏返回按钮标题的详细分析。

 NSArray *viewControllerArray = [self.navigationController viewControllers];
    // get index of the previous ViewContoller
    long previousIndex = [viewControllerArray indexOfObject:self] - 1;
    if (previousIndex >= 0) {
        UIViewController *previous = [viewControllerArray objectAtIndex:previousIndex];
        previous.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc]
                                                         initWithTitle:backButtonTitle
                                                         style:UIBarButtonItemStylePlain
                                                         target:self
                                                         action:nil];
    }

如果你有多个导航

ParentViewController—> ChildViewController1—> ChildViewController2

您可以使用下面的代码来更改导航栏上后退按钮的标题。

self.navigationController .navigationBar.topItem ?。backBarButtonItem = UIBarButtonItem。init(标题:"Back",样式:.plain,目标:nil,动作:nil)