Matt
Matt

Reputation: 2930

Pass NSMutableArray to another view controller - not working

I have a view controller PlayerViewController, I am trying to pass a NSMutableArray: stories, to the view controller PlaylistViewController

in the PlayerViewController.h file I have

@class PlaylistViewController;

in the PlayerViewController.m file I have

//stories is a NSMutableArray that does have content in it
PlaylistViewController *sVC = [[PlaylistViewController alloc] initWithNibName:@"PlaylistViewController" bundle:nil];
    sVC.playSongArray = [[NSMutableArray alloc]init];
    sVC.playSongArray = stories; 

in the PlaylistViewController.h file I have

NSMutableArray *playSongArray;

and

@property(nonatomic,retain)NSMutableArray *playSongArray;

I also synthesize it in the .m file

But when I run the code, playSongArray in PlaylistViewController is empty. What am I doing wrong?

Upvotes: 0

Views: 2758

Answers (3)

Chris Wagner
Chris Wagner

Reputation: 21013

How are you presenting this view? I've seen multiple times where I had to present the view before setting properties like this in order for them to be applied properly. It seems the the view is not full initialized until it is presented/displayed and part of the initialization process sets the properties to nil.

Assuming you are presenting this modally try the following order instead.

//stories is a NSMutableArray that does have content in it
PlaylistViewController *sVC = [[PlaylistViewController alloc] initWithNibName:@"PlaylistViewController" bundle:nil];
[self presentModalViewController:svc animated:YES]; // if modal
[self pushViewController:svc animated:YES]; // if part of a uinavigationcontroller
sVC.playSongArray = [[NSMutableArray alloc]init];
sVC.playSongArray = stories; 

Upvotes: 1

Abhi Beckert
Abhi Beckert

Reputation: 33389

I suspect you are checking the value of playSongArray too early. Your PlaylistViewController should look like:

@interface PlaylistViewContror {
  NSMutableArray *playSongArray;
}

- (id)initWithPlaySongArray:(NSMutableArray *)array;

@property(nonatomic,retain)NSMutableArray *playSongArray;

@end

@implementation PlaylistViewContror

@synthesize playSongArray;

- (id)initWithPlaySongArray:(NSMutableArray *)array
{
  if (!(self = [super initWithNibName:@"PlaylistViewController" bundle:nil]))
    return nil;

  playSongArray = array;

  return self;
}

- (void)awakeFromNib
{
  // do things with playSongArray here
}

@end

And then in your PlayerViewController, you just do:

//stories is a NSMutableArray that does have content in it
PlaylistViewController *sVC = [[PlaylistViewController alloc] initWithPlaySongArray:stories];

Upvotes: 0

Niko
Niko

Reputation: 2543

Instead of :

sVC.playSongArray = [[NSMutableArray alloc]init];
sVC.playSongArray = stories; 

Try

sVC.playSongArray = [[NSMutableArray alloc]initWithArray:stories]; 

If it's not working, in your PlaylistViewController controller, add playSongArray = [[NSMutableArray alloc]init]; in initWithNibName function then create a function :

- (void)setPlaySongs:(NSArray *)songs {
[playSongArray addObjectsFromArray:songs];
}

and load your view controller this way :

PlaylistViewController *sVC = [[PlaylistViewController alloc] initWithNibName:@"PlaylistViewController" bundle:nil];
[sVC setPlaySongs:stories];

Upvotes: 0

Related Questions