How to return to the previous view in Objective-C?

I am starting to program iOS, and I want to implement the functionality of returning to a home look.

I am using this code:

-(IBAction)onclickhome:(id)sender { [self.navigationController popViewControllerAnimated:YES]; } 

I used navigationController in the home mouse click, but did not go to the main screen.

Do you have any suggestions and source code for my code?

+6
source share
3 answers

I'm not sure I understand your question.

What do you have in mind

I used navigationController in the home mouse click, but this does not go to the main screen

?

If you want to go to the root controller, you can use popToRootViewControllerAnimated .

 [self.navigationController popToRootViewControllerAnimated:YES]; 

From Apple UINavigationController Doc

 popToRootViewControllerAnimated: Pops all the view controllers on the stack except the root view controller and updates the display. - (NSArray *)popToRootViewControllerAnimated:(BOOL)animated 

If you configured the UINavigationController , and its root controller is called A, then if you go from A to B, and then from B to C, you have two options to return to the previous controller (you can have others but I list the main ones):

  • go from C to B using popViewControllerAnimated
  • go from C to A using popToRootViewControllerAnimated
+8
source

The best way to move back and forth is to pop and display views from the navigation controller stack.

To return to one view, use the code below. This will ensure a smooth transition between views.

 UINavigationController *navigationController = self.navigationController; [navigationController popViewControllerAnimated:YES]; 

To go back to the two views, do as shown below

 UINavigationController *navigationController = self.navigationController; [navigationController popViewControllerAnimated:NO]; [navigationController popViewControllerAnimated:YES]; 

If you do not return to the expected view, execute the code below before any of the views appear ...

 UINavigationController *navigationController = self.navigationController; NSLog(@"Views in hierarchy: %@", [navigationController viewControllers]); 

You should see an array like the one below that will help you check the stack, as expected.

 Views in hierarchy: ( "<Main_ViewController: 0x1769a3b0>", "<First_ViewController: 0x176a5610>", "<Second_ViewController: 0x176af180>" 
+6
source

I use

 [self dismissViewControllerAnimated:YES completion:nil]; 

as other answers did not work in Xcode 8.0

+2
source

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


All Articles