问题
I'm struggling to pass a NSMutable array from a Modal View controller back to the view controller I came from.
This is my current method:
FirstViewController.h
#import "SecondViewController.h"
@property (strong, nonatomic) IBOutlet NSMutableArray *passedRecipientsArray;
FirstViewController.m
@synthesize passedRecipientsArray = _passedRecipientsArray;
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"passedRecipientsArray: %@", self.passedRecipientsArray);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"addContact"]){
UINavigationController *nav = [segue destinationViewController];
SecondViewController *secondViewController = (SecondViewController *)nav.topViewController;
secondViewController.emailContact = @"TRUE";
}
}
SecondViewController.h
@property (strong, nonatomic) IBOutlet NSMutableArray *selectedContactsArray;
SecondViewController.m
@synthesize passedRecipientsArray = _passedRecipientsArray;
- (void)closeWindow
{
if([self.selectedContactsArray count] != 0){
NSLog(@"PASS ME: %@", self.selectedContactsArray);
FirstViewController *firstViewController = [[FirstViewController alloc] init];
if(firstViewController.passedRecipientsArray == nil) firstViewController.passedRecipientsArray = [[NSMutableArray alloc] init];
firstViewController.passedRecipientsArray = self.selectedContactsArray;
[self dismissModalViewControllerAnimated:YES];
}
}
Is there a better way of doing this? I've tried to used this: How to pass object on modal view's dismissal but get very confused.
Does anyone have a good tutorial/clear easy way of doing what I'm after? Can anyone tell me where I'm going wrong?
回答1:
Don't allocate FirstViewController inside the SecondViewController. Because FirstViewController is your parent class.Old FirstViewController objects will be null after the re-allocation
Pass the FirstViewController instance instead of writing
FirstViewController *firstViewController = [[FirstViewController alloc] init];
Example:
SecondViewController.h
#import "FirstViewController.h"
FirstViewController *firstViewController;
@property (strong, nonatomic) IBOutlet NSMutableArray *selectedContactsArray;
@property (strong, nonatomic) FirstViewController *firstViewController;
SecondViewController.m
@synthesize passedRecipientsArray = _passedRecipientsArray;
@synthesize firstViewController;
- (void)closeWindow
{
if([self.selectedContactsArray count] != 0){
if(self.firstViewController.passedRecipientsArray == nil)
self.firstViewController.passedRecipientsArray = self.selectedContactsArray;
[self dismissModalViewControllerAnimated:YES];
}
}
Then modify your FirstViewController as
SecondViewController *secondViewController;
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"addContact"]){
UINavigationController *nav = [segue destinationViewController];
secondViewController = (SecondViewController *)nav.topViewController;
secondViewController.emailContact = @"TRUE";
secondViewController.firstViewController = self;
}
}
回答2:
Firstly do not create and allocate another instance of firstViewController in secondViewController..instead..create a property FirstViewController *firstViewController
in secondViewController further synthesize it in secondViewController .m file...
follow the rectified code
FirstViewController.h
#import "SecondViewController.h"
@property (strong, nonatomic) IBOutlet NSMutableArray *passedRecipientsArray;
FirstViewController.m
@synthesize passedRecipientsArray = _passedRecipientsArray;
- (void)viewDidAppear:(BOOL)animated {
NSLog(@"passedRecipientsArray: %@", self.passedRecipientsArray);
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if([[segue identifier] isEqualToString:@"addContact"]){
UINavigationController *nav = [segue destinationViewController];
SecondViewController *secondViewController = (SecondViewController *)nav.topViewController;
secondViewController.firstViewController = self; // u should create firstViewController first in secondViewController class making it a property
secondViewController.emailContact = @"TRUE";
}
}
then in secondViewController
SecondViewController.h
@interface FirstViewController : UIViewController{
FirstViewController *firstViewController;
}
@property (strong, nonatomic) IBOutlet NSMutableArray *selectedContactsArray;
@property(nonatomic,strong) FirstViewController *firstViewController;
SecondViewController.m
@synthesize passedRecipientsArray = _passedRecipientsArray;
@synthesize firstViewController
- (void)closeWindow
{
if([self.selectedContactsArray count] != 0){
NSLog(@"PASS ME: %@", self.selectedContactsArray);
if(firstViewController.passedRecipientsArray == nil) {
firstViewController.passedRecipientsArray = [[NSMutableArray alloc] init];
firstViewController.passedRecipientsArray = self.selectedContactsArray;
[self dismissModalViewControllerAnimated:YES];
}
}
}
回答3:
I am just wondering why don't you add protocol to your model view? You can set NSMutableArray in model view and then get it from parent view.
来源:https://stackoverflow.com/questions/12911115/passing-nsmutablearray-from-modal-view-controller-to-parent-view