Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to press "Back" button in UINavigationController programmatically

I have a UIViewController called FriendsViewController inside a UINavigationController. And a second UIViewController called FriendsDetailedViewController. When navigating from the first view controller to the second, I want to programmatically press the Back button when needed. How to do this?

like image 987
Timur Mustafaev Avatar asked Oct 04 '11 09:10

Timur Mustafaev


People also ask

How do I make a back button in Swift?

Back-button text is taken from parent view-controller's navigation item title. So whatever you set on previous view-controller's navigation item title, will be shown on current view controller's back button text. You can just put "" as navigation item title in parent view-controller's viewWillAppear method.

What is Interactivepopgesturerecognizer?

The gesture recognizer responsible for popping the top view controller off the navigation stack.


3 Answers

Simply use

[self.navigationController popViewControllerAnimated:YES]

from FriendsDetailedViewController. Your view will be popped out i.e. the behavior of back button.

Note that it returns UIViewController on normally, and returns nil if there is nothing to pop.

like image 87
Mann Avatar answered Oct 19 '22 18:10

Mann


If by pressing "Back" button you mean just to go to the previous view controller, you can just call:

[self.navigationController popViewControllerAnimated:YES];
like image 16
Bartek Avatar answered Oct 19 '22 18:10

Bartek


Here is the swift method

if let navController = self.navigationController {
    navController.popViewControllerAnimated(true)
}
like image 9
MGM Avatar answered Oct 19 '22 20:10

MGM