Navigate Back to previous view controller

70,175

Solution 1

You need to use:

[self.navigationController popToRootViewControllerAnimated:YES];

This will bring you back to the root view controller.

If you want to navigate back to previous view controller, you should implement:

[self.navigationController popViewControllerAnimated:YES];

Solution 2

By using below line we can go to parent view controller

[self.navigationController popViewControllerAnimated:YES]; 

By using below line we can move to main/root view controller

[self.navigationController popToRootViewControllerAnimated:YES]; 

By using below line we can move to any view controller

[self.navigationController popToViewController:viewControllerObject animated:YES]; 

Solution 3

How about...

 [self.navigationController dismissViewControllerAnimated:YES completion:NULL];

Assuming that you are currently in a navigation-based controller and you wanted to go back to the previous controller before you got into a navigation-based controller.

Solution 4

With swift3,

@IBAction func back(_ sender: UIButton) {
    self.dismiss(animated: true, completion: nil)     
}

Solution 5

Swift solutions for easy copy pasting:

navigationController?.popViewControllerAnimated(true)
Share:
70,175
user1688346
Author by

user1688346

Updated on July 10, 2022

Comments

  • user1688346
    user1688346 almost 2 years

    I have a UIView->UICollectionView->UICollectionViewCell. I am trying to navigate back programatically but none of these works. The code did called. I am using StoryBoard.

    - (void) goBack:(NSNotification *) notification {
          // [self.navigationController popViewControllerAnimated:YES];
         //  [self dismissViewControllerAnimated:YES completion:nil];
          [self.navigationController popToRootViewControllerAnimated:YES];
    }
    
  • Vineet Singh
    Vineet Singh almost 7 years
    Have you set UINavigationController as your rootViewController ? If not, then it will not work.