在 UINavigationController 中为根视图制作后退按钮
因此,我手动创建了一个UINavigationController,将其设置为UIWindow的 rootViewController,并且我想使用后退按钮退出UINavigationController并加载另一个 viewController 代替它。但是,UINavigationBar的backItem属性是只读,所以我不知道如何正确设置它(它是只读 并且在根导航视图中默认为 nil)。我怎样才能实现这个(或类似的效果,我希望能够通过按根视图上的后退按钮有效地“退出”这个UINavigationController)。
或者,这是不好的形式吗?我应该如何转义 UINavigationController 的根视图?
编辑:
使用以下代码尝试Legolas的解决方案:(某些名称已更改)
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];
但按钮不显示。我做错了什么?其他后退按钮显示正常,但这个按钮仍然无法显示。
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).
Alternatively, is this bad form? How should I escape the root view of a UINavigationController?
EDIT:
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以通过不同的方法来完成此操作。
转到方法:
隐藏后退按钮
通过创建新的 UIButton 或 UIBarButtonItem
,并将其放置在后退按钮的位置。然后,您可以在单击按钮时使用操作
更新我的答案:在 viewDidLoad 方法中使用它 // 就像一个魅力 //
You can do this in a different approach.
Go to the method:
Hide the back button with
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
Updating my answer: Use this in the viewDidLoad method //works like a charm //
我能想到的有几种方法。
leftBarButtonItem
。leftBarButtonItem
。UINavigationController
堆栈中。此根视图的唯一目的是启动辅助视图,或者在用户返回时终止。这样,您将永远不会看到没有后退按钮的UINavigationController
。我已经测试过这个(但没有分析它);性能影响似乎可以忽略不计。另外,请查看 <https://discussions.apple.com/message/8298537#8298537>>从 2008 年开始。同样的问题。
对此,有人回复:
There are a few approaches I can think of.
leftBarButtonItem
.leftBarButtonItem
.UINavigationController
stack. The sole purpose of this root view will be to launch the secondary view, or die when the user comes back to it. This way, you will never seeUINavigationController
without a back button. I've tested this (but not profiled it); performance impact seems negligible.Also, check out <https://discussions.apple.com/message/8298537#8298537> from 2008. Same question.
To which someone replied:
尝试一下:-
try dis:-