从任何 UIViewcontroller 返回主菜单
我想知道是否有任何方法可以从 UIViewController
返回,例如后退 3-4 步,我有一个主屏幕,可以通过 导航到其他
,在下一个视图中,它将有一个UIViewController
现在ModalViewControllerUINavigationBar
,它将导航到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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
让根级别视图控制器注册为通知的观察者,例如“POP_TO_ROOT”。当它收到此通知时,调用一个方法来关闭模态视图控制器(或堆栈上第一个控制器)。
在您的视图控制器堆栈中,任何 4 或 5 级别的视图都可以发布通知“POP_TO_ROOT”。
编辑:添加代码
在调用presentModalViewController之前在主“屏幕”中执行以下操作:
并添加此方法:
然后,在视图控制器层次结构的深处,当您想要完全弹出时,
你只需要发布一个通知:
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:
and add this method:
Then, down deep in your viewcontroller hierarchy, when you want to pop all the way out,
you just need to post a notification:
如果我正确理解您的问题,您将从“主视图控制器”模态地呈现一个导航控制器(附有根视图控制器),并且您希望能够返回到“主视图控制器”。
因为您始终有指向导航控制器的指针,所以您应该能够调用
dismissModalViewControllerAnimated:
从任何视图控制器中,它都会带您回到主视图控制器。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.将根视图控制器保存在某些属性中并调用:
- (NSArray *)popToViewController:(UIViewController *)viewController 动画:(BOOL)动画
Save your root View Controller in some property and call:
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated