将NSMutableArray传递给另一个视图控制器

我想在两个视图控制器之间传递一个NSMutableArray。 请让我知道我能做些什么

在我有PlaylistViewController.h文件

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

我希望传递给另一个视图控制器

您可以通过两种方式共享:

  1. 使用属性

.h文件中

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

.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. 使用NSUserDefaults

  [[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; 

假设你想从其他视图控制器传递NSMutableArrayPlaylistViewController让我们说viewcontroller .m然后做下面的视图controller.m

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

你可以设置视图控制器你希望传递数组作为原始视图控制器的委托(在你的情况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 } 

制作你的NSMutableArray属性并合成它。

这是在你的课程的对象之后。

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

在secondViewController中创build一个NSMutableArray属性secondMutArray。 在firstViewController中,你想传递mutablearray的地方,创build第二个viewcontroller的实例,并将self.mutableArray赋值给secondMutArray。 喜欢这个

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