在UINavigationController中为根视图制作返回按钮 [英] Making back button for root view in UINavigationController
问题描述
所以我手动创建了一个 UINavigationController ,将其设置为我的 UIWindow的 rootViewController,我想使用后退按钮退出 UINavigationController 并在其位置加载另一个viewController。但是, UINavigationBar 的 backItem 属性是 readonly ,因此我不知道如何正确设置它(它只是 readonly 并在根导航视图中默认为nil。我怎样才能实现这一点(或者类似的效果,我希望能够通过按下根视图上的后退按钮有效地退出这个 UINavigationController 。)
So I created a UINavigationController manually, set it as my UIWindow's rootViewController, and I would like to use a back button to exit the UINavigationController and load another viewController in its place. However, the backItem property of the UINavigationBar is readonly, so I don't know how to set it properly (it is readonly and defaults to nil in the root navigation view). How can I achieve this (or similar effect, I want to be able to effectively "exit" this UINavigationController by pressing the back button on the root view).
或者,这是不好的形式?我应该如何逃避 UINavigationController 的根视图?
Alternatively, is this bad form? How should I escape the root view of a UINavigationController?
编辑:
使用以下代码尝试Legolas的解决方案:(某些名称已更改)
Attempted Legolas' solution with the following code: (some names have been changed)
UINavigationController* navController = [[UINavigationController alloc] initWithRootViewController:plvc]; // plvc being the first viewcontroller
MyAppDelegate* appDelegate = [Utility getAppDelegate];
appDelegate.window.rootViewController = navController;
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStyleBordered target:self action:@selector(initializeStuff)];
navController.navigationItem.leftBarButtonItem = backButton;
[navController.navigationItem setHidesBackButton:YES animated:YES];
[navController.view setNeedsDisplay];
但按钮不显示。我究竟做错了什么?其他后退按钮显示正常,但这个按钮仍然没有显示。
But the button does not display. What am I doing wrong? The other back buttons display properly, but this one still does not.
推荐答案
您可以采用不同的方法。
You can do this in a different approach.
转到方法:
- (void)viewDidLoad
隐藏后退按钮
[self.navigationItem setHidesBackButton:YES animated:YES];
创建一个新的UIButton或UIBarButtonItem,并将其放在后退按钮的位置。
Create a new UIButton or a UIBarButtonItem, and place it in place of the back button.
然后您可以在单击按钮时使用操作
You can then use an action when you click the button
- (IBAction) clickBackButton : (id) sender; //and push view controller to your required view.
更新我的答案:在viewDidLoad方法中使用//就像魅力//
Updating my answer: Use this in the viewDidLoad method //works like a charm //
[self.navigationItem setHidesBackButton:YES animated:YES];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStyleBordered target:self action:@selector(initializeStuff)];
self.navigationItem.leftBarButtonItem = backButton;
这篇关于在UINavigationController中为根视图制作返回按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!