从任何 UIViewcontroller 返回主菜单

发布于 2024-12-27 07:24:30 字数 251 浏览 1 评论 0原文

我想知道是否有任何方法可以从 UIViewController 返回,例如后退 3-4 步,我有一个主屏幕,可以通过 导航到其他 UIViewController现在ModalViewController,在下一个视图中,它将有一个UINavigationBar,它将导航到4-5级深度。我想放置一个按钮,让用户直接返回主页,而无需返回他输入的所有视图。

提前谢谢。

I'm wondering if there is any way to return from UIViewController like 3-4 step backward, I've a main screen which will navigate to other UIViewController via presentModalViewController, on the next view, it will have a UINavigationBar which will navigate to a 4-5 level deeps. i wanna to put a button that let the user go back to the home directly without returning for all the view he enter.

thx in advance.

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

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

发布评论

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

评论(3

青衫儰鉨ミ守葔 2025-01-03 07:24:30

让根级别视图控制器注册为通知的观察者,例如“POP_TO_ROOT”。当它收到此通知时,调用一个方法来关闭模态视图控制器(或堆栈上第一个控制器)。

在您的视图控制器堆栈中,任何 4 或 5 级别的视图都可以发布通知“POP_TO_ROOT”。

编辑:添加代码

在调用presentModalViewController之前在主“屏幕”中执行以下操作:

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(handlePopToRoot) 
                                                 name:@"POP_TO_ROOT"
                                               object:nil];

并添加此方法:

 - (void) handlePopToRoot {
     [[NotificationCenter defaultCenter] removeObserver:self 
                                                   name:@"POP_TO_ROOT" 
                                                 object:nil];
     [self.navigationController dismissModalViewControllerAnimated: YES]; 
 }

然后,在视图控制器层次结构的深处,当您想要完全弹出时,
你只需要发布一个通知:

 [[NSNotificationCenter defaultCenter] postNotification:@"POP_TO_ROOT" object:nil];

Have your root level view controller register as an observer of a notification, such as "POP_TO_ROOT". When it receives this notification, call a method to dismiss your modal view controller (or whatever is first on the stack).

In your viewcontroller stack, any of the views 4 or 5 levels in can just post a notification "POP_TO_ROOT".

EDIT: add code

In your main "screen" before you call presentModalViewController, do this:

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(handlePopToRoot) 
                                                 name:@"POP_TO_ROOT"
                                               object:nil];

and add this method:

 - (void) handlePopToRoot {
     [[NotificationCenter defaultCenter] removeObserver:self 
                                                   name:@"POP_TO_ROOT" 
                                                 object:nil];
     [self.navigationController dismissModalViewControllerAnimated: YES]; 
 }

Then, down deep in your viewcontroller hierarchy, when you want to pop all the way out,
you just need to post a notification:

 [[NSNotificationCenter defaultCenter] postNotification:@"POP_TO_ROOT" object:nil];
我不会写诗 2025-01-03 07:24:30

如果我正确理解您的问题,您将从“主视图控制器”模态地呈现一个导航控制器(附有根视图控制器),并且您希望能够返回到“主视图控制器”。

因为您始终有指向导航控制器的指针,所以您应该能够调用
dismissModalViewControllerAnimated: 从任何视图控制器中,它都会带您回到主视图控制器。

    [[self.navigationController parentViewController] dismissModalViewControllerAnimated:YES]

If I understand your question correctly, you are presenting a navigation controller (with a root view controller attached to it) from your "main view controller" modally, and you want to be able to get back to your "main view controller".

Because you will always have pointer to your navigation controller, you should be able to call
dismissModalViewControllerAnimated: from any of your view controllers and it will take you right back to the main view controller.

    [[self.navigationController parentViewController] dismissModalViewControllerAnimated:YES]
小…红帽 2025-01-03 07:24:30

将根视图控制器保存在某些属性中并调用:
- (NSArray *)popToViewController:(UIViewController *)viewController 动画:(BOOL)动画

Save your root View Controller in some property and call:
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated

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