Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop the current view using Segues/Storyboard on iOS 5

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 ?

like image 482
Jukurrpa Avatar asked Jun 14 '12 14:06

Jukurrpa


People also ask

What are segues in iOS?

Segues are visual connectors between view controllers in your storyboards, shown as lines between the two controllers. They allow you to present one view controller from another, optionally using adaptive presentation so iPads behave one way while iPhones behave another.

How do I get a storyboard view controller?

Open Main. storyboard and select the Tab Bar Controller Scene. On the right, select the Attribute inspector. You'll find a checkbox named Is Initial View Controller.

How do I add segue to iOS?

To create a segue between view controllers in the same storyboard file, Control-click an appropriate element in the first view controller and drag to the target view controller. The starting point of a segue must be a view or object with a defined action, such as a control, bar button item, or gesture recognizer.


4 Answers

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.

like image 195
rickster Avatar answered Oct 10 '22 16:10

rickster


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.

like image 27
NSZombie Avatar answered Oct 10 '22 14:10

NSZombie


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];
like image 16
John Ballinger Avatar answered Oct 10 '22 16:10

John Ballinger


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

like image 3
iJames Avatar answered Oct 10 '22 15:10

iJames