-3

I am trying to pass a NSMutableArray between two view controller. Please let me know what can i do for this

In the PlaylistViewController.h file I have

NSMutableArray *SongArray;
@property(nonatomic,retain)NSMutableArray *SongArray;

which I wish to pass to another view controller

Dobroćudni Tapir
  • 3,082
  • 20
  • 37

6 Answers6

9

You can share in two ways:

  1. Using property

Example

In .h file

    @interface ABCController : UIViewController
    {
        NSMutableArray *detailArray;
    }
    @property(nonatomic,retain)NSMutableArray *detailArray;

In .m file

    XYZController *xyzVC = [[XYZController alloc] initWithNibName:@"XYZController" bundle:nil];    
    xyzVC.detailArray = self.detailArray;

    [self.navigationController pushViewCsecondView:xyzVC animated:YES];


    **XYZController.h**

    @interface XYZController : UIViewController
    {
        NSMutableArray *detailArray;
    }

@property(nonatomic,retain)NSMutableArray *detailArray;
  1. Using NSUserDefaults

Example

     [[NSUserDefaults standardUserDefaults] setValue:SongArray forKey:@"songArray"];
     [[NSUserDefaults standardUserDefaults] synchronize];

     NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"songArray"];
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
7
**FirstViewController.h**

@interface FirstViewController : UIViewController
{
    NSMutableArray *SongArray;
}
@property(nonatomic,retain)NSMutableArray *SongArray;

**FirstViewController.m**

SecondViewController *secondView = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil];    
secondView.SongArray = self.SongArray;

[self.navigationController secondView animated:YES];


**SecondViewController.h**

@interface SecondViewController : UIViewController
{
    NSMutableArray *SongArray;
}
@property(nonatomic,retain)NSMutableArray *SongArray;
V.J.
  • 9,492
  • 4
  • 33
  • 49
1

Suppose you want to pass NSMutableArray to PlaylistViewController from some other view controller lets say viewcontroller.m then do following in view controller.m

PlaylistViewController *play=[[PlaylistViewController alloc]initwithnibname:@"PlaylistViewController"];

play.SongArray=arrayofSongsWhichYouWantToPass;

[self.navigationController pushViewController:play animated:YES];
Dharmbir Singh
  • 17,485
  • 5
  • 50
  • 66
Pankaj Wadhwa
  • 3,073
  • 3
  • 28
  • 37
1

You could set view controller you wish to pass array to as a delegate of origin view controller (in your case PlaylistViewController)

**OriginViewController.h**

@protocol OriginViewControllerDelegate {
-(void)passMutableArray:(NSMutableArray*)array;
}

@interface OriginViewController : UIViewController

@property(nonatomic,retain)id<OriginViewControllerDelegate> delegate;
@property(nonatomic,retain)NSMutableArray *array;

**OriginViewController.m**

//set DestinationViewController as delegate to OriginViewController(not necessarily in OriginViewController.m
//When you want to pass array just send message to delegate
[self.delegate passMutableArray:array];

**DestinationViewController.h**

@interface DestinationViewController : UIViewController <OriginViewControllerDelegate>
//implement protocol function in your m file

**DestinationViewController.m**

-(void)passMutableArray:(NSMutableArray*)array {
//Do whatever you want with the array
}
Dobroćudni Tapir
  • 3,082
  • 20
  • 37
0

Make property of your NSMutableArray and synthesize it.

And this after making object of your class.

PlaylistViewController *PLVC = [[PlaylistViewController alloc] init];
PLVC.SongArray=yourAry;
[self.navigationController pushViewController:PLVC animated:YES];
Rajneesh071
  • 30,846
  • 15
  • 61
  • 74
0

create one NSMutableArray property secondMutArray in secondViewController. in firstViewController ,where you want to pass mutablearray , there create the instance of 2nd viewcontroller & assign the self.mutableArray to secondMutArray. like this

SecondViewController *secondViewController=[[SecondViewController alloc]init];
secondViewController.secondMutArray=self.mutableArray
Ravindra Bagale
  • 17,226
  • 9
  • 43
  • 70