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
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…