Transmettre NSMutableArray à un controller de vue à un autre

J'essaye de passer un NSMutableArray entre deux controller de vue. S'il vous plaît laissez-moi savoir ce que je peux faire pour cela

Dans le file PlaylistViewController.h j'ai

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

que je souhaite passer à un autre controller de vue

Vous pouvez partager de deux façons:

  1. Utilisation de la propriété

Exemple

Dans le file .h

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

Dans le file .m

  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. Utilisation de NSUserDefaults

Exemple

  [[NSUserDefaults standardUserDefaults] setValue:SongArray forKey:@"songArray"]; [[NSUserDefaults standardUserDefaults] synchronize]; NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"songArray"]; 
 **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; 

Supposons que vous vouliez passer NSMutableArray à PlaylistViewController partir d'un autre controller de vue permet de dire viewcontroller .m puis de faire suite à la vue controller.m

 PlaylistViewController *play=[[PlaylistViewController alloc]initwithnibname:@"PlaylistViewController"]; play.SongArray=arrayofSongsWhichYouWantToPass; [self.navigationController pushViewController:play animated:YES]; 

Vous pouvez définir le controller de vue sur lequel vous souhaitez passer le tableau en tant que controller de vue de délégué d'origine (dans votre cas 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 } 

Faites la propriété de votre NSMutableArray et synthétisez-la.

Et ceci après avoir fait l'object de votre class.

 PlaylistViewController *PLVC = [[PlaylistViewController alloc] init]; PLVC.SongArray=yourAry; [self.navigationController pushViewController:PLVC animated:YES]; 

créez une propriété NSMutableArray secondMutArray dans secondViewController. Dans firstViewController, où vous voulez passer mutablearray, créez l'instance de 2nd viewcontroller et affectez le self.mutableArray à secondMutArray. comme ça

 SecondViewController *secondViewController=[[SecondViewController alloc]init]; secondViewController.secondMutArray=self.mutableArray