我试图在两个视图控制器之间传递一个 NSMutableArray。请让我知道我能为此做些什么
在 PlaylistViewController.h 文件中我有
NSMutableArray *SongArray;
@property(nonatomic,retain)NSMutableArray *SongArray;
我希望传递给另一个视图控制器
我试图在两个视图控制器之间传递一个 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
给.m 然后在视图控制器.m 中执行以下操作PlaylistViewController
viewcontroller
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
}
在 secondViewController 中创建一个 NSMutableArray 属性 secondMutArray。在要传递 mutablearray 的 firstViewController 中,创建第二个 viewcontroller 的实例并将 self.mutableArray 分配给 secondMutArray。像这样
SecondViewController *secondViewController=[[SecondViewController alloc]init];
secondViewController.secondMutArray=self.mutableArray
制作你的财产NSMutableArray
并合成它。
这在成为你的班级的对象之后。
PlaylistViewController *PLVC = [[PlaylistViewController alloc] init];
PLVC.SongArray=yourAry;
[self.navigationController pushViewController:PLVC animated:YES];