在 UINavigationController 中为根视图制作后退按钮

发布于 2024-11-14 12:41:51 字数 1126 浏览 1 评论 0原文

因此,我手动创建了一个UINavigationController,将其设置为UIWindow的 rootViewController,并且我想使用后退按钮退出UINavigationController并加载另一个 viewController 代替它。但是,UINavigationBarbackItem属性是只读,所以我不知道如何正确设置它(它是只读 并且在根导航视图中默认为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

剩余の解释 2024-11-21 12:41:51

您可以通过不同的方法来完成此操作。

转到方法:

- (void)viewDidLoad

隐藏后退按钮

[self.navigationItem setHidesBackButton:YES animated:YES];

通过创建新的 UIButton 或 UIBarButtonItem

,并将其放置在后退按钮的位置。然后,您可以在单击按钮时使用操作

- (IBAction) clickBackButton : (id) sender; //and push view controller to your required view.

更新我的答案:在 viewDidLoad 方法中使用它 // 就像一个魅力 //

[self.navigationItem setHidesBackButton:YES animated:YES];
UIBarButtonItem* backButton = [[UIBarButtonItem alloc] initWithTitle:@"Start" style:UIBarButtonItemStyleBordered target:self action:@selector(initializeStuff)];

self.navigationItem.leftBarButtonItem = backButton;

You can do this in a different approach.

Go to the method:

- (void)viewDidLoad

Hide the back button with

[self.navigationItem setHidesBackButton:YES animated:YES];

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.

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;
踏雪无痕 2024-11-21 12:41:51

我能想到的有几种方法。

  1. 使用您自己设计的“后退按钮”图标或系统默认图标。使用leftBarButtonItem
  2. 制作默认图标的图像。再次使用 leftBarButtonItem
  3. 创建一个假根视图,并将其推入 UINavigationController 堆栈中。此根视图的唯一目的是启动辅助视图,或者在用户返回时终止。这样,您将永远不会看到没有后退按钮的 UINavigationController。我已经测试过这个(但没有分析它);性能影响似乎可以忽略不计。

另外,请查看 <https://discussions.apple.com/message/8298537#8298537>>从 2008 年开始。同样的问题。

问题是,用户如何退出 UINavigationController 并返回到应用程序?根导航栏没有后退按钮或任何类型的退出挂钩。

对此,有人回复:

要执行您想做的操作,技巧是将“超级根”控制器放入导航控制器中,但将导航控制器的 navigationBarHidden 属性在 viewWillAppear 中设置为 YES,在 viewWillDisappear 中设置为 NO。 (为了获得奖励积分,请在适当的时候将其设置为动画。)

There are a few approaches I can think of.

  1. Use your own "back button" icon of your own design, or a system default. Use leftBarButtonItem.
  2. Make an image of the default icon. Again, use leftBarButtonItem.
  3. Make a fake root view that you push into the 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 see UINavigationController 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.

The problem is, how does the user get out of a UINavigationController and back to the app? The root navigation bar doesn't have a back button or any sort of hook to exit.

To which someone replied:

To do what you want to do, the trick is to put the "super-root" controller inside the navigation controller, but have it set the nav controller's navigationBarHidden property to YES in viewWillAppear and NO in viewWillDisappear. (For bonus points, animate it when appropriate.)

梦在深巷 2024-11-21 12:41:51

尝试一下:-

UIBarButtonItem *leftBarButton  = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(initializeStuff)];
[self.navigationItem setleftBarButtonItem:leftBarButton];
[leftBarButton release];

try dis:-

UIBarButtonItem *leftBarButton  = [[UIBarButtonItem alloc] initWithTitle:@"back" style:UIBarButtonItemStylePlain target:self action:@selector(initializeStuff)];
[self.navigationItem setleftBarButtonItem:leftBarButton];
[leftBarButton release];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文