ios 无法更改后退按钮标题,但我可以隐藏它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9276002/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't change back button title but I can hide it
提问by VansFannel
I'm developing an iPhoneapplication with latest SDKand XCode 4.2
我正在使用最新的 SDK和XCode 4.2开发iPhone应用程序
I use a UINavigationController, and to show a new ViewController I do this on AppDelegate.m:
我使用 UINavigationController,并显示一个新的 ViewController,我在 AppDelegate.m 上执行此操作:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPhone" bundle:nil];
}
else
{
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController_iPad" bundle:nil];
}
self.viewController.title = NSLocalizedString(@"MAIN", nil);
navController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
navController.navigationBar.tintColor = [UIColor darkGrayColor];
self.window.rootViewController = navController;
[self.window makeKeyAndVisible];
return YES;
}
- (void) openDocumentation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone)
{
docViewController = [[DocumentationViewController alloc] initWithNibName:@"DocumentationViewController_iPhone" bundle:nil];
}
else
{
docViewController = [[DocumentationViewController alloc] initWithNibName:@"DocumentationViewControllerr_iPad" bundle:nil];
}
docViewController.title = NSLocalizedString(@"DOCUMENTATION", nil);
self.viewController.navigationItem.backBarButtonItem.title = @"Back";
[navController pushViewController:docViewController animated:YES];
}
On DocumentationViewController.m
I do this:
在DocumentationViewController.m
我这样做:
- (void) viewWillAppear:(BOOL)animated
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
}
But it doesn't change back button title.
但它不会改变后退按钮的标题。
I move that code to viewDidLoad:
我将该代码移动到 viewDidLoad:
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view from its nib.
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
}
- (void) viewWillAppear:(BOOL)animated
{
}
But still doesn't work.
但仍然不起作用。
And this doesn't work either:
这也不起作用:
- (void) viewWillAppear:(BOOL)animated
{
self.navigationItem.backBarButtonItem.title = @"Back";
}
But if I do this, to test if I can't access back button:
但是如果我这样做,要测试我是否无法访问后退按钮:
- (void) viewWillAppear:(BOOL)animated
{
self.navigationItem.hidesBackButton = YES;
}
It works!! And hides back button.
有用!!并隐藏后退按钮。
What am I doing wrong?
我究竟做错了什么?
回答by yuji
The back button title shown by a UINavigationViewController
doesn't depend on the navigationItem.backBarButtonItem.title
of the topmost view controller in the stack. It depends on the second topmostview controller's value of navigationItem.backBarButtonItem.title
.
a 显示的后退按钮标题UINavigationViewController
不依赖于navigationItem.backBarButtonItem.title
堆栈中最顶层视图控制器的标题。这取决于第二个最上面的视图控制器的值navigationItem.backBarButtonItem.title
。
So you need to set navigationItem.backBarButtonItem.title
for whatever view controller is showing when you push your DocumentViewController
.
因此,navigationItem.backBarButtonItem.title
当您推送DocumentViewController
.
回答by Brody Robertson
When you set the backBarButtonItem title of a UIViewController, you are actually setting the title for the back button if this UIViewController is the second controller in the UINavigationController stack.
当您设置 UIViewController 的 backBarButtonItem 标题时,如果此 UIViewController 是 UINavigationController 堆栈中的第二个控制器,则您实际上是在设置后退按钮的标题。
self.navigationItem.backBarButtonItem.title = @"Custom Title";
So, if the above view controller is already in the nav controller and subsequently pushes a child view controller into the nav controller, the back button title will be "Custom Title".
因此,如果上面的视图控制器已经在导航控制器中并且随后将子视图控制器推入导航控制器,则后退按钮标题将是“自定义标题”。
回答by pvllnspk
Just add in the Storyboard or Nib file your custom button (this will replace the navigation button) and add a back action for it:
只需在 Storyboard 或 Nib 文件中添加您的自定义按钮(这将替换导航按钮)并为其添加后退操作:
- (IBAction)back:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
For me it allows to customize my back button.
对我来说,它允许自定义我的后退按钮。
回答by rordulu
Actually you can do this with just one trick:
实际上,您只需一个技巧即可做到这一点:
Override UINavigationBar class and add this line of code:
覆盖 UINavigationBar 类并添加以下代码行:
- (void)layoutSubviews{
self.backItem.title = @"";
[super layoutSubviews];
}
Then initialize your UINavigationController with this custom UINavigationBar class.. etc.
然后用这个自定义 UINavigationBar 类初始化你的 UINavigationController ......等等。
UINavigationController * navController = [[UINavigationController alloc] initWithNavigationBarClass:[CBCNavigationBar class] toolbarClass:nil]
UINavigationController * navController = [[UINavigationController alloc] initWithNavigationBarClass:[CBCNavigationBar class] toolbarClass:nil]
Hope this helps
希望这可以帮助