IOS ViewController 导航路径 - 如何以编程方式向后导航
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8697501/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS ViewController navigation path - how to navigate back programatically
提问by mpprdev
I'm using IOS5 Storyboard. My View Controller path is as follows:
我正在使用 IOS5 故事板。我的视图控制器路径如下:
tabbarVC --> navigationVC-1 --> tableVC-1 --(via segue push)-> tableVC-2 --(via segue modal)-> navigationVC-2 --> tableVC-3
In the cancel button callback action method in tableVC-3 I call [self dismissViewControllerAnimated:YES completion:nil];
that successfully gets me back to tableVC-2. However when I try to examine the navigation path backwards in the debugger, I don't see a way to access tableVC-2 from navigationVC-2. I expected navigationVC-2 to maintain a link to tableVC-2 or navigationVC-1 but it doesn't seem to. Please see my debugger output below.
在 tableVC-3 中的取消按钮回调操作方法中,我调用[self dismissViewControllerAnimated:YES completion:nil];
它成功让我回到 tableVC-2。但是,当我尝试在调试器中向后检查导航路径时,我没有看到从 navigationVC-2 访问 tableVC-2 的方法。我希望 navigationVC-2 保持指向 tableVC-2 或 navigationVC-1 的链接,但它似乎没有。请在下面查看我的调试器输出。
Can someone explain the navigation hierarchy and how to traverse the chain backwards programatically?
有人可以解释导航层次结构以及如何以编程方式向后遍历链吗?
(gdb) po self
<tableVC-3: 0x6d33340>
(gdb) po (UIViewController*) [self navigationController]
<UINavigationController: 0x6d33560>
(gdb) po (UIViewController*)[[self navigationController] navigationController]
Can't print the description of a NIL object.
(gdb) po (UIViewController*)[[self navigationController] topViewController]
<tableVC-3: 0x6d33340>
(gdb) po (UIViewController*)[[self navigationController] presentingViewController]
<UITabBarController: 0x6b2eba0>
(gdb) po (UIViewController*)[[self navigationController] presentedViewController]
Can't print the description of a NIL object.
(gdb) po (UIViewController*)[[self navigationController] visibleViewController]
<tableVC-3: 0x6d33340>
回答by Mike Gledhill
This is an old question, but just to help anyone else who comes across this issue, there's a single command which'll make your life easier..
这是一个老问题,但只是为了帮助遇到此问题的其他人,有一个命令可以让您的生活更轻松..
[self.navigationController popToRootViewControllerAnimated:TRUE];
Easy when you stumble across the right command, isn't it !
当您偶然发现正确的命令时很容易,不是吗!
So, supposing you had a series of three screens in a Navigation Controller, and on the thirdscreen you wanted the "Back" button to take you back to the initialscreen.
因此,假设您在导航控制器中有一系列三个屏幕,并且在第三个屏幕上您希望“返回”按钮将您带回初始屏幕。
-(void)viewDidLoad
{
[super viewDidLoad];
// change the back button and add an event handler
self.navigationItem.leftBarButtonItem =
[[UIBarButtonItem alloc] initWithTitle:@"Back"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(handleBack:)];
}
-(void)handleBack:(id)sender
{
NSLog(@"About to go back to the first screen..");
[self.navigationController popToRootViewControllerAnimated:TRUE];
}
回答by Adam Knights
After some research using this and a couple other questions for modal UIViewControllers in storyboard to go back two views I used
在使用这个和一些其他关于情节提要中的模态 UIViewControllers 的问题进行一些研究后,返回我使用的两个视图
[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
回答by Vincent Bernier
Going throw an old response tough of updating it to be more complet.
To address this question :
将抛出一个旧的响应,很难更新它以使其更加完整。
要解决这个问题:
Can someone explain the navigation hierarchy and how to traverse the chain backwards programatically?
有人可以解释导航层次结构以及如何以编程方式向后遍历链吗?
The structure of your navigation :
您的导航结构:
tabbarVC --> navigationVC-1 --> tableVC-1 --(via segue push)-> tableVC-2 --(via segue modal)-> navigationVC-2 --> tableVC-3
tabbarVC --> navigationVC-1 --> tableVC-1 --(通过segue push)-> tableVC-2 --(通过segue modal)-> navigationVC-2 --> tableVC-3
Can be explain like this :
The TabbarVC is showing it's 'selectedViewController' (navigationVC-1).
NavigationVC-1 has its navigation stack compose of TableVC-1 and TableVC-2 (topViewController of NavigagtionVC-1)
Then NavigationVC-2 is presented Modally over the tabbarVC, so tabbarVC is the presentingViewController
and NavigationVC-2 is the presentedViewController
可以这样解释:
TabbarVC 显示它是“selectedViewController”(navigationVC-1)。
NavigationVC-1 的导航堆栈由 TableVC-1 和 TableVC-2(NavigagtionVC-1 的 topViewController)组成,
然后 NavigationVC-2 以模态呈现在 tabbarVC 上,因此 tabbarVC 是presentingViewController
NavigationVC-2 是presentedViewController
So in order to reach tableVC-2 from tableVC-3 you would need to do something like this :
因此,为了从 tableVC-3 到达 tableVC-2,您需要执行以下操作:
[(UINavigationController *)[(UITabBarController *)[tableVC-3 presentingViewController] selectedViewController] topViewController];
(don't do that in production code)
(不要在生产代码中这样做)
[tableVC-3 presentingViewController]
as well as [tableVC-3.navigationController presentingViewController]
will give you back the UITabBarController
.
[tableVC-3 presentingViewController]
以及[tableVC-3.navigationController presentingViewController]
会给你回UITabBarController
。
If you are using a UINavigationControlleryou should use it's push
and pop
method to put UIViewController on or off the "presentation stack".
You will be able to access the UINavigationController
from those UIViewController
like this:
如果您正在使用UINavigationController,您应该使用它的push
和pop
方法将 UIViewController 放在或关闭“演示堆栈”。
您将能够UINavigationController
从这样的人访问UIViewController
:
self.navigationController
If you want to go back more than one UIViewController
on the "presentation stack" you can use this method on the UINavigationController
如果您想UIViewController
在“演示堆栈”上返回多个,您可以在UINavigationController
popToViewController:animated:
Pops view controllers until the specified view controller is at the top of the navigation stack.
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated
popToViewController:animated:弹出
视图控制器,直到指定的视图控制器位于导航堆栈的顶部。
- (NSArray *)popToViewController:(UIViewController *)viewController animation:(BOOL)animated
To dismiss a UIViewControllerthat was presented modally, the UIViewController
that have presented it need to dismiss it with :
要关闭以模态呈现的UIViewController,已经呈现的UIViewControllerUIViewController
需要关闭它:
- (void)dismissModalViewControllerAnimated:(BOOL)animated
So in this case it should be :
所以在这种情况下它应该是:
[tableVC-2 dismissModalViewControllerAnimated:YES];
回答by Suragch
Swift
迅速
If you are using a navigation controller the you can navigate back to the previous view controller with
如果您使用的是导航控制器,则可以使用以下命令导航回上一个视图控制器
self.navigationController?.popViewControllerAnimated(true)
or back to the root view controller with
或返回根视图控制器
self.navigationController?.popToRootViewControllerAnimated(true)