How to pop current view controller (non modal) and go back to previous one?

16,218

Solution 1

this will pop the top controller from navigation controller stack!

 [self.navigationController popViewControllerAnimated:YES];

and if you are not aware of UINavigationController and its uses then follow this link

http://www.idev101.com/code/User_Interface/UINavigationController/

Solution 2

You need to use a UINavigationController. This maintains a stack of your view controllers. You can modify your view stack by calling pushViewController:animated and popViewController:animated.

Initialize your instance of UINavigationController with a rootViewController (your first view controller), and then present your UINavigationController in some way, then all you need is to play around with your UINavigationController instance.

Solution 3

if you are pushing the view controller use:

      [self.navigationController popViewControllerAnimated:NO];

else if you are using a modal transition use:

      [self dismissViewControllerAnimated:YES completion:^{
          //Stuff after dismissing
        }];
Share:
16,218
Pra Do
Author by

Pra Do

Updated on July 22, 2022

Comments

  • Pra Do
    Pra Do almost 2 years

    I am trying to use a custom back button on my View Controller in Objective-C to pop the current view controller so that i can go back to previous controller. All the controllers are just view controllers and are not modally presented. How do I programmatically pop the current view controller (non modal) and go back to previous one?

  • Pra Do
    Pra Do almost 10 years
    it worked by using dismissviewcontroller. Actually i was using regular view controllers so it worked.