Programmatically call navigation controller back button on iOS

In an UINavigationController based iPhone application, the method I would like to execute is the software equivalent of pressing the back button and back button.

i.e. automatically click the "Jobs" button, as shown here:

Navigation Controller image

Is there a general iOS call that I can make, or is more information needed?

+84
ios objective-c iphone button uinavigationcontroller
Jan 21 '10 at 8:44
source share
5 answers

UINavigationController -popViewControllerAnimated: method should do what you want:

 [navigationController popViewControllerAnimated:YES]; 
+188
Jan 21
source share

Assuming you really don't want to press the button programmatically, but just copy the result of the button click, you have to tell the navigation controller to display the current controller.

[self.navigationController popViewControllerAnimated:YES];

This will remove it from the stack and return you to the previous view controller.

+24
Jan 21 '10 at 9:31
source share

Swift 3.0

Return to root view

 self.navigationController?.popToRootViewController(animated: true) 

Back to previous view

 self.navigationController?.popViewController(animated: true) 

Swift 2.3

Return to root view

 self.navigationController?.popToRootViewControllerAnimated(true) 

Back to previous view

 self.navigationController?.popViewControllerAnimated(true) 
+21
Dec 26 '15 at
source share

You have to call

popViewControllerAnimated:

which is the opposite of adding view controllers using pushViewController:animated:

+7
Jan 21 2018-10-21T00:
source share
 [self.navigationController popViewControllerAnimates:YES]; 

is the best option, but if you are neither the same class as the controller of the controller, nor your delegate changes before calling the back button method, you can also try -

you must first define the return button ---

 UIBarButtonItem *newBackButton = [[UIBarButtonItem alloc] initWithTitle: @"anyTitleForBackButton" style: UIBarButtonItemStyleBordered target: nil action: @selector(backButtonTapped)]; [[self navigationItem] setBackBarButtonItem: newBackButton]; [newBackButton release]; 

and then in the backButtonTapped method you can call -

 [self.navigationController pushViewController:desiredViewController animated:YES]; 
+6
Dec 14 '11 at 1:06 p.m.
source share



All Articles