IOS ViewController导航路径 - 如何以编程方式导航

时间:2012-01-02 04:52:48

标签: ios uinavigationcontroller ios5 xcode-storyboard

我正在使用IOS5 Storyboard。我的视图控制器路径如下:

tabbarVC --> navigationVC-1 --> tableVC-1 --(via segue push)-> tableVC-2 --(via segue modal)-> navigationVC-2 --> tableVC-3

在tableVC-3中的取消按钮回调动作方法中,我调用成功让我回到tableVC-2的[self dismissViewControllerAnimated:YES completion:nil];。但是,当我尝试在调试器中向后检查导航路径时,我没有看到从navigationVC-2访问tableVC-2的方法。我期望navigationVC-2保持到tableVC-2或navigationVC-1的链接,但似乎没有。请参阅下面的调试器输出。

有人可以解释导航层次结构以及如何以编程方式向后遍历链吗?

(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>

4 个答案:

答案 0 :(得分:8)

这是一个老问题,但只是为了帮助遇到此问题的其他人,只需一个命令就可以让您的生活更轻松。

[self.navigationController popToRootViewControllerAnimated:TRUE];

当你偶然发现正确的命令时很容易,不是吗!

因此,假设您在导航控制器中有一系列三个屏幕,并且在第三个​​屏幕上,您希望“返回”按钮将您带回初始屏幕。

-(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];
}

答案 1 :(得分:4)

抛出一个旧的反应很难将其更新为更完整。
要解决这个问题:

  

有人可以解释导航层次结构以及如何以编程方式向后遍历链吗?

导航结构:

  

tabbarVC - &gt; navigationVC-1 - &gt; tableVC-1 - (通过segue推) - &gt; tableVC-2 - (通过segue模态) - &gt; navigationVC-2 - &gt; tableVC-3

可以这样解释:
TabbarVC显示它的'selectedViewController'(navigationVC-1) NavigationVC-1的导航堆栈由TableVC-1和TableVC-2组成(NavigagtionVC-1的topViewController)
然后,NavigationVC-2在tabbarVC上进行模态显示,因此tabbarVC为presentingViewController,NavigationVC-2为presentedViewController

因此,为了从tableVC-3到达tableVC-2,您需要执行以下操作:

[(UINavigationController *)[(UITabBarController *)[tableVC-3 presentingViewController] selectedViewController] topViewController];

(不要在生产代码中这样做)

[tableVC-3 presentingViewController]以及[tableVC-3.navigationController presentingViewController]会返回UITabBarController


如果您使用UINavigationController,则应使用pushpop方法将UIViewController打开或关闭“演示文稿堆栈”。
您可以从这些UINavigationController访问UIViewController,如下所示:

self.navigationController

如果您想在“演示文稿堆栈”上返回多个UIViewController,可以在UINavigationController上使用此方法

  

popToViewController:动画:
  弹出视图控制器,直到指定的视图控制器位于导航堆栈的顶部        - (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated


要解除以模态方式呈现的UIViewController,提交该UIViewController的{​​{1}}需要将其解雇:

- (void)dismissModalViewControllerAnimated:(BOOL)animated

所以在这种情况下它应该是:

[tableVC-2 dismissModalViewControllerAnimated:YES];

答案 2 :(得分:4)

经过一些研究,在故事板中使用此模型和模态UIViewControllers的其他几个问题,回过头来看两个视图

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];

答案 3 :(得分:0)

夫特

如果您使用的是导航控制器,则可以使用

导航回上一个视图控制器
self.navigationController?.popViewControllerAnimated(true)

或使用

返回根视图控制器
self.navigationController?.popToRootViewControllerAnimated(true)