我想在兩個視圖控制器之間傳遞一個NSMutableArray。請讓我知道我可以爲這個將NSMutableArray傳遞給另一個視圖控制器
做在PlaylistViewController.h文件我有
NSMutableArray *SongArray;
@property(nonatomic,retain)NSMutableArray *SongArray;
我希望傳遞到另一個視圖控制器
我想在兩個視圖控制器之間傳遞一個NSMutableArray。請讓我知道我可以爲這個將NSMutableArray傳遞給另一個視圖控制器
做在PlaylistViewController.h文件我有
NSMutableArray *SongArray;
@property(nonatomic,retain)NSMutableArray *SongArray;
我希望傳遞到另一個視圖控制器
您可以通過兩種方式分享:
實施例
在.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;
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;
讓您NSMutableArray
的財產和合成它。
而且這是在你的課程成爲對象之後。
PlaylistViewController *PLVC = [[PlaylistViewController alloc] init];
PLVC.SongArray=yourAry;
[self.navigationController pushViewController:PLVC animated:YES];
在secondViewController中創建一個NSMutableArray屬性secondMutArray。 在firstViewController中,你想傳遞mutablearray,那裏創建第二個viewcontroller的實例&將self.mutableArray賦值給secondMutArray。 這樣
SecondViewController *secondViewController=[[SecondViewController alloc]init];
secondViewController.secondMutArray=self.mutableArray
假設你想從其他視圖控制器通過NSMutableArray
到PlaylistViewController
可以說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
}