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 returns nil 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)
}
Share:
34,131
Timur Mustafaev
Author by

Timur Mustafaev

Updated on July 08, 2022

Comments

  • Timur Mustafaev
    Timur Mustafaev almost 2 years

    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?