当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前左边栏按钮的默认值是加载当前视图的视图的标题,换句话说,当按下按钮(后退按钮)时显示的视图。
我想把按钮上显示的文本更改为其他内容。
我试着把下面这行代码放在视图控制器的viewDidLoad方法中,但它似乎不起作用。
self.navigationItem.leftBarButtonItem.title = @"Log Out";
我该怎么办?
谢谢。
当前回答
如果你不仅想将后退按钮的文本更改为相同的文本并保持原来的左箭头形状,而且还想在用户单击后退按钮时做一些事情,我建议你看看我的“CustomNavigationController”。
其他回答
好的。我个人讨厌所有这些选择。因此我想出了我自己的。
根据我所看到的信息。看起来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;
}
我觉得这是一种更优雅的方式。
我把这些代码放在一个地方,在需要时自动填充。
不需要在每个推送请求之前调用代码。
希望这能有所帮助
根据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];
}
我发现,更改返回按钮名称最简单的方法是将视图控制器的标题设置为返回按钮的标题,然后将视图控制器导航项中的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是显示的。
编辑:这也保持了后退按钮的原始外观(左边带箭头的栏按钮项)。
在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的期望和回来。