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

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

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

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

我该怎么办?

谢谢。


当前回答

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

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

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

其他回答

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的期望和回来。

也许我过于简单了,但从苹果的文档中,措辞是:

如果一个自定义栏按钮项没有被任何一个视图控制器指定,则使用一个默认的返回按钮,并且它的标题被设置为前一个视图控制器的title属性的值——也就是说,在堆栈的下一层的视图控制器。

上面标记为正确的解决方案设置了来自父控制器的默认按钮项。这是正确的答案,但我正在通过改变自我来解决问题。在将新控制器推送到NavigationController堆栈之前,UIViewController的title属性。

这将自动更新后退按钮在下一个控制器上的标题,只要你设置了self。标题回到什么它应该在viewWillAppear我不能看到这个方法造成太多的问题。

使用下面的代码行:

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

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

self.navigationItem.leftItemsSupplementBackButton = YES;

下面是backBarButtonItem的文档:

时,此导航项位于顶部项的正下方 堆栈时,导航控制器派生返回按钮 此导航项中的导航栏。[…如果你想的话 为后退按钮指定自定义图像或标题,则可以指定 自定义栏按钮项目(与您的自定义标题或图像)到此 财产。”

视图控制器A(父视图控制器):

self.title = @"Really Long Title";
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Short" style:UIBarButtonItemStyleBordered target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;

当任何其他视图控制器B在导航堆栈的顶部,而A在它的正下方时,B的后退按钮的标题将是“Short”。

这里解释的解决方案没有一个对我有效。所以我所做的就是从我来自的场景中删除标题,方法如下:

自我。标题= @"";

因此,当新场景呈现时,后面的文本不会出现。

我完全同意,这不是一个明确的解决方案,但工作,没有一个解释对我有效。