如何处理按下的 backBarButtonItem?

发布于 2024-12-04 11:52:58 字数 271 浏览 1 评论 0原文

我几乎在所有应用程序中都完成了此操作,但是我在 navigationController 中堆叠了 3 个视图,并且我需要从第三个视图跳转到第一个视图。 据我了解,我只能通过 viewWillDisappear 来做到这一点。但是如果我尝试这个“跳转”,我将从第二个视图中获取 navigationController 面板,该面板带有导致异常/错误的导航按钮。

PS 不要建议我让 leftBarButtonitem 看起来像 backBarButtonItem。这太难了,我不知道在哪里可以找到合适的图像。

I have almost done this in all the application but I have 3 views stacked in navigationController and I need to jump from the third view to the first view.
As I understand I can do this via viewWillDisappear only. But if I try this "jump" I will get the navigationController panel from the second View which with a navigation buttons which cause exceptions/errors.

P.S. Do not advice me to make leftBarButtonitem looking like backBarButtonItem. It is too difficult and I don't know where to find an appropriate image for it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

蹲墙角沉默 2024-12-11 11:52:58

据我所知,您别无选择,只能提供自己的 UIBarButtonItem。默认情况下,您不允许中断 UINavigationController 的工作方式。也就是说,您无法覆盖后退按钮的行为。您必须提供自定义栏按钮项并将其设置为导航项的左栏按钮项。

(顺便说一句,您正在寻找的行为可能表明导航模式不佳。后退按钮几乎应该总是按顺序退出导航层次结构。)

To my knowledge, you have no choice but to provide your own UIBarButtonItem. You are not permitted from interrupting how UINavigationController works by default. That is, you cannot override the behavior of the back button. You must provide a custom bar button item and set it as the navigation item's left bar button item.

(As a side note, the sort of behavior you're looking for may be an indication of a poor navigation pattern. Back buttons should almost always back out of a navigation hierarchy sequentially.)

云裳 2024-12-11 11:52:58

假设按照导航顺序,您的视图像顶部一样堆叠 -> 3-> 2-> 1.当您处于此位置时,您可以在应用程序委托中有一个标志,显示按下 backButton 时您将 doublePop,如下所示:(每当第三个视图按您提到的顺序出现时,您就会执行此操作)

MyApplicationDelegate * del = [[UIApplication sharedApplication]delegate];
del.doublePopEnabled = YES;
[del release];

在视图 2 中:

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
         MyApplicationDelegate * del = [[UIApplication sharedApplication]delegate];
         if(del.doublePopEnabled){
           //Asssuming you have a reference to your navigationController in your view 2
           del.doublePopEnabled = NO;
           [del.release]
           //Use animated as no if you don't want user to see doublePopping.
           self.navigationController popViewControllerAnimated:NO];

          }
 }

希望它有所帮助。

Let's say in navigation order your views stacked like top -> 3 -> 2 -> 1 . When you are in this position you can have a flag in your application delegate that shows you will doublePop when backButton pressed as below: ( You are doing this whenever third view appears in the order you mentioned)

MyApplicationDelegate * del = [[UIApplication sharedApplication]delegate];
del.doublePopEnabled = YES;
[del release];

In view 2 :

- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
         MyApplicationDelegate * del = [[UIApplication sharedApplication]delegate];
         if(del.doublePopEnabled){
           //Asssuming you have a reference to your navigationController in your view 2
           del.doublePopEnabled = NO;
           [del.release]
           //Use animated as no if you don't want user to see doublePopping.
           self.navigationController popViewControllerAnimated:NO];

          }
 }

Hope it helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文