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
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];
**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;
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
You can share in two ways:
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;
NSUserDefaults
Example
[[NSUserDefaults standardUserDefaults] setValue:SongArray forKey:@"songArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"songArray"];
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
}
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];