0

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?

Community
  • 1
  • 1
davidford.me
  • 824
  • 1
  • 15
  • 24

3 Answers3

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;
    }
}
Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102
  • I'm now getting an error on `@property (strong, nonatomic) FirstViewController *firstViewController;` saying 'Unknown Type name FirstViewController' and 'Property with retain (or strong) attribute must be type' – davidford.me Oct 16 '12 at 10:24
1

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];
            }
        }
    }
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27
0

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.

brianLikeApple
  • 4,262
  • 1
  • 27
  • 46
  • @DavidFord I give you link tutorial, just follow it. What I learn protocol is from here as well. [How to pass parameters from one view to another view](http://www.theappcodeblog.com/2011/04/15/passing-data-between-views-tutorial-using-a-protocol-delegate-in-your-iphone-app/) – brianLikeApple Oct 16 '12 at 10:50