我正在使用最新SDK 和 XCode 4.2
开发 iPhone 应用程序我使用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];
}
在DocumentationViewController.m
我执行此操作:
- (void) viewWillAppear:(BOOL)animated
{
UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStylePlain target:nil action:nil];
self.navigationItem.backBarButtonItem = backButton;
}
但它不会改变按钮标题。
我将该代码移至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
{
}
但仍然无效。
这也不起作用:
- (void) viewWillAppear:(BOOL)animated
{
self.navigationItem.backBarButtonItem.title = @"Back";
}
但如果我这样做,要测试我是否无法访问后退按钮:
- (void) viewWillAppear:(BOOL)animated
{
self.navigationItem.hidesBackButton = YES;
}
有效!!并隐藏了按钮。
我做错了什么?
答案 0 :(得分:19)
UINavigationViewController
显示的后退按钮标题不依赖于堆栈中最顶层视图控制器的navigationItem.backBarButtonItem.title
。它取决于第二个最顶层视图控制器的navigationItem.backBarButtonItem.title
值。
因此,当您推送navigationItem.backBarButtonItem.title
时,您需要设置DocumentViewController
以显示控制器显示的内容。
答案 1 :(得分:6)
答案 2 :(得分:2)
当您设置UIViewController的backBarButtonItem标题时,如果此UIViewController是UINavigationController堆栈中的第二个控制器,则实际上是设置后退按钮的标题。
self.navigationItem.backBarButtonItem.title = @"Custom Title";
因此,如果上面的视图控制器已经在导航控制器中并随后将子视图控制器推入导航控制器,则后退按钮标题将为“自定义标题”。
答案 3 :(得分:1)
只需在Storyboard或Nib文件中添加自定义按钮(这将替换导航按钮)并为其添加后退操作:
- (IBAction)back:(id)sender {
[self.navigationController popViewControllerAnimated:YES];
}
对我而言,它允许自定义我的后退按钮。
答案 4 :(得分:0)
实际上你只需要一个技巧就可以做到这一点:
覆盖UINavigationBar类并添加以下代码行:
- (void)layoutSubviews{
self.backItem.title = @"";
[super layoutSubviews];
}
然后使用此自定义UINavigationBar类初始化您的UINavigationController ..等等
UINavigationController * navController = [[UINavigationController alloc] initWithNavigationBarClass:[CBCNavigationBar class] toolbarClass:nil]
希望这有帮助