Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Pass information Back in iOS when reversing a Segue?

I have two view controllers, One and Two. I go from VC One to VC Two. On VC Two, I select some data that I store in an array. When I press the "Back" button on the navigation bar, I would like to send that array back to VC One.

What's the best way to do this?

Thanks!

like image 662
user1797508 Avatar asked Nov 27 '12 14:11

user1797508


People also ask

How do you pass data backwards in Swift?

To pass data back, the most common approach is to create a delegate property in your detail view controller, like this: class ViewControllerB: UIViewController { var selectedName: String = "Anonymous" weak var delegate: ViewControllerA! }

How to unwind a segue?

In storyboard, go to the view that you want to unwind from and simply drag a segue from your button or whatever up to the little orange "EXIT" icon at the top right of your source view. That's it, your segue will unwind when your button is tapped.

How to call unwind segue programmatically?

To trigger an unwind segue programmatically (or any other segue), we need to give it an identifier in the storyboard. There is no arrow in a storyboard for unwind segues. To select one, you have to find it in the document outline on the left side of Interface Builder.


2 Answers

Why not set up a delegate property on your second view controller that the first can register as. Then when the information is stored to the array, it can also be passed back to it's delegate?

To implement this

At the top of your second view controllers .h file, you'll need to declare an @protocol that the first view controller can implement. A protocol is simular to an interface in other languages. It's a way of being sure an object implements certain methods, without needing to know specifically what that object is (view controller 1 in this case).

@protocol MyDataDelegate

- (void)recieveData:(NSArray *)theData

@end

and also declare a property for the delegate that the first view controller can set it's self as before it presents the second

@interface SecondViewController

@property (nonatomic, weak) id<MyDataDelegate> delegate;

Then in your first view controller .h file, implement the protocol as so

in the .h file

#import SecondViewController.h

@interface FirstViewController <MyDataDelegate>

//.....

and in the .m, implement the methods declared in the protocol

@implementation

//.... usual methods

- (void)recieveData:(NSArray *)theData {

    //Do something with data here
}

In order to set the first view controller as the delegate, you need to intercept the segue before it happens, by using a UIStoryBoardDelegate method. Add this to the first view controller

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    //Get a handle on the view controller about be presented
    SecondViewController *secondViewController = segue.destinationViewController;

    if ([secondViewController isKindOfClass:[SecondViewController class]]) {
        secondViewController.delegate = self;
    }
}

Now you have a pointer to the first view controller from the second, and can call methods and pass back the data, by calling the following method in the second view controller

[self.delegate recieveData:theArrayData];

You could also add another method to the protocol to notify the delegate that the second view controller is being dismissed if you wanted. Or use some of the suggestions from the other answers

like image 145
Sam Clewlow Avatar answered Sep 27 '22 18:09

Sam Clewlow


If you are using iOS 6 you can use UnwindSegues to return information back down the stack.

like image 32
Abizern Avatar answered Sep 27 '22 17:09

Abizern