How to press "Back" button in UINavigationController programmatically
34,131
Solution 1
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 returnsnil
if there is nothing to pop.
Solution 2
If by pressing "Back" button you mean just to go to the previous view controller, you can just call:
[self.navigationController popViewControllerAnimated:YES];
Solution 3
Here is the swift method
if let navController = self.navigationController {
navController.popViewControllerAnimated(true)
}
Solution 4
Swift 5
self.navigationController?.popViewController(animated: true)
Usage in a code snippet:
func unwindViewController() {
self.navigationController?.popViewController(animated: true)
}
Author by
Timur Mustafaev
Updated on July 08, 2022Comments
-
Timur Mustafaev almost 2 years
I have a
UIViewController
calledFriendsViewController
inside aUINavigationController
. And a secondUIViewController
calledFriendsDetailedViewController
. When navigating from the first view controller to the second, I want to programmatically press theBack
button when needed. How to do this?