Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IOS ViewController navigation path - how to navigate back programatically

I'm using IOS5 Storyboard. My View Controller path is as follows:

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.

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>
like image 222
mpprdev Avatar asked Dec 05 '22 18:12

mpprdev


2 Answers

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 third screen you wanted the "Back" button to take you back to the initial screen.

-(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];
}
like image 146
Mike Gledhill Avatar answered Dec 15 '22 01:12

Mike Gledhill


After some research using this and a couple other questions for modal UIViewControllers in storyboard to go back two views I used

[self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil];
like image 40
Adam Knights Avatar answered Dec 15 '22 00:12

Adam Knights