IOS ViewController Navigation Path - How to Programmatically Navigate Backward

I am using the iOS5 storyboard. The path of My View Controller 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 method in tableVC-3, I call [self dismissViewControllerAnimated:YES completion:nil]; which successfully returns me to the tableVC-2. However, when I try to check the navigation path back in the debugger, I see no way to access tableVC-2 from navigationVC-2. I was expecting navigationVC-2 to support a reference to TableVC-2 or navigationVC-1, but that doesn't look like it. See below the output of my debugger.

Can someone explain the navigation hierarchy and how to trace the chain back programmatically?

 (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
source share
4 answers

This is an old question, but to help someone else who is facing this problem, there is one team that will make your life easier.

 [self.navigationController popToRootViewControllerAnimated:TRUE]; 

Easy when you stumble on the right team, right?

So, suppose you had a series of three screens in the navigation controller, and on the third screen you would like the back button to return you 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]; } 
+8
source

Switching to the old answer hard updates it to be more complete.
To resolve this issue:

Can someone explain the navigation hierarchy and how to trace the chain back programmatically?

The structure of your navigation:

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

You can explain it like this:
TabbarVC shows its 'selectedViewController' (navigationVC-1).
NavigationVC-1 has its own navigation stack consisting of TableVC-1 and TableVC-2 (topViewController of NavigagtionVC-1)
Then NavigationVC-2 is presented Modically over tabbarVC, so tabbarVC is presentingViewController , and NavigationVC-2 is presentedViewController

So, in order to reach tableVC-2 from tableVC-3, you will need to do something like this:

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

(do not do this in production code)

[tableVC-3 presentingViewController] , as well as [tableVC-3.navigationController presentingViewController] will return you a UITabBarController .


If you are using the UINavigationController , you must use the push and pop method to enable or disable the UIViewController "view stack".
You will be able to access the UINavigationController from these UIViewController as follows:

 self.navigationController 

If you want to return to more than one UIViewController in the presentation stack, you can use this method on the UINavigationController

popToViewController: animated:
Controls the controllers until the specified view controller is at the top of the navigation stack.
- (NSArray *) popToViewController: (UIViewController *) viewController animated: (BOOL) animated


To reject a UIViewController that was presented modally, the UIViewController that introduced it should reject it with:

 - (void)dismissModalViewControllerAnimated:(BOOL)animated 

So, in this case it should be:

 [tableVC-2 dismissModalViewControllerAnimated:YES]; 
+4
source

After some research using this and a couple of other questions for the modal UIViewControllers in the storyboard, to return to the two views, I used

 [self.presentingViewController.presentingViewController dismissViewControllerAnimated:YES completion:nil]; 
+4
source

Swift

If you are using a navigation controller, you can return to the previous view controller using

 self.navigationController?.popViewControllerAnimated(true) 

or back to the root view controller using

 self.navigationController?.popToRootViewControllerAnimated(true) 
0
source

Source: https://habr.com/ru/post/1388883/


All Articles