Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
981 views
in Technique[技术] by (71.8m points)

xcode - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...