Pop the current view using Segues/Storyboard on iOS 5

36,875

Solution 1

You could try calling [self dismissViewControllerAnimated:YES completion:nil]; from the controller you want to dismiss (whether the controller has been pushed, or shown modally).

Here is the related documentation : UIViewController Class Reference

The presenting view controller is responsible for dismissing the view controller it presented. If you call this method on the presented view controller itself, it automatically forwards the message to the presenting view controller.

Solution 2

Storyboards in iOS 5 don't provide a "no-code" way to return from a segue -- that's something you'll need to implement yourself.

If you use "push" segues (which require a navigation controller), use the navigation controller's popViewControllerAnimated: method to undo the last push segue. (Or other methods to undo more; see the UINavigationController documentation.)

If you use "modal" segues, call dismissViewControllerAnimated:completion: on the view controller which presented the current view controller (which you can get from its presentingViewController property).


Update: In iOS 6 and later there's unwind segues for going "back" in a storyboard. It's still not a no-code solution -- and it shouldn't be, because you need to be able to do things like differentiating between "Done" and "Cancel" exits from a modal view controller. But it does let you put more of the semantic flow of your app into the storyboard. Apple has a tech note that describes them in detail, and they're also covered in the video from WWDC 2012 Session 407.

Solution 3

Just to clarify.

In the class that was pushed. Simply wire up the following and the controller and view will be popped off.

[self.navigationController popViewControllerAnimated:YES];

Solution 4

Create Segue type "Custom" on your stroyboard. This can be from a button. Create a new UIStoryboardSegue class named "popSegue" In the popSegue.m file add the following;

-(void)perform{
    UIViewController *sourceViewContreoller = [self sourceViewController];
    [sourceViewContreoller.navigationController popViewControllerAnimated:YES];
}

-In the storyboard editor.

-Select the segue and change the Segue Class to "popSegue"

-Set the Identifier to "popSegue"

Done!

You can use the same "popSegue" class throughout your project.

Hope this helps

Solution 5

I'm using Xcode 5 also and here's how it's done. First, in the view code file that pushed the other, create an IBAction method in the .h file such as this:

- (IBAction)exitToHere:(UIStoryboardPopoverSegue *)segue sender:(id)sender;

Then in the .m file add this:

- (IBAction)exitToHere:(UIStoryboardPopoverSegue *)segue sender:(id)sender {

}

You can add any cleanup code you want executed in this method. Next go to your storyboard and select the pushed view. I assume you've got some kind of button on the view that the user taps to signal he's finished. Click on that button, hold down the key and drag to the the green box below the view which is the Exit. Release the mouse button but continue to hold the key. A popup will appear and your method will show in the list. Select that method. Now when the user clicks on the button, the view will pop and you'll be returned to the starting method.

Share:
36,875
Jukurrpa
Author by

Jukurrpa

Updated on July 09, 2022

Comments

  • Jukurrpa
    Jukurrpa almost 2 years

    I am creating an app using iOS 5 SDK. I managed to push views using the Storyboard's Segues, but I cannot find the proper way to pop the current view and go back to the previous one.

    I am not using any navigationController (the app doesn't have any top or bottom bars).

    I don't think using modal or push segue the other way would be the solution as it instantiates a new controller.

    Do I have to use a custom Segue with the opposite animation and deletion of the view at the end ? Or is there a better way ?

  • Ben Mosher
    Ben Mosher almost 12 years
    Note: this does not work w/ navigation controllers (to pop).
  • androniennn
    androniennn about 11 years
    Great ;). You made my day :). Thank you