xcode 将 NSMutableArray 传递给一个视图控制器到另一个
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16142078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pass NSMutableArray to one view controller to another
提问by nivrit gupta iphone
I am trying to pass a NSMutableArray between two view controller. Please let me know what can i do for this
我试图在两个视图控制器之间传递一个 NSMutableArray。请让我知道我可以为此做些什么
In the PlaylistViewController.h file I have
在 PlaylistViewController.h 文件中,我有
NSMutableArray *SongArray;
@property(nonatomic,retain)NSMutableArray *SongArray;
which I wish to pass to another view controller
我希望传递给另一个视图控制器
回答by Dharmbir Singh
You can share in two ways:
您可以通过两种方式分享:
- Using property
- 使用财产
Example
例子
In .hfile
在.h文件中
@interface ABCController : UIViewController
{
NSMutableArray *detailArray;
}
@property(nonatomic,retain)NSMutableArray *detailArray;
In .mfile
在.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;
- Using
NSUserDefaults
- 使用
NSUserDefaults
Example
例子
[[NSUserDefaults standardUserDefaults] setValue:SongArray forKey:@"songArray"];
[[NSUserDefaults standardUserDefaults] synchronize];
NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"songArray"];
回答by V.J.
**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;
回答by pankaj
Suppose you want to pass NSMutableArrayto PlaylistViewControllerfrom some other view controller lets say viewcontroller.m then do following in view controller.m
假设你想从其他一些视图控制器传递NSMutableArray给.m 然后在 view controller.m 中执行以下操作PlaylistViewControllerviewcontroller
PlaylistViewController *play=[[PlaylistViewController alloc]initwithnibname:@"PlaylistViewController"];
play.SongArray=arrayofSongsWhichYouWantToPass;
[self.navigationController pushViewController:play animated:YES];
回答by Dobro?udni Tapir
You could set view controller you wish to pass array to as a delegate of origin view controller (in your case PlaylistViewController)
您可以将您希望将数组传递给的视图控制器设置为原始视图控制器的委托(在您的情况下为 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
}
回答by Rajneesh071
Make property of your NSMutableArrayand synthesize it.
使你的财产NSMutableArray和综合。
And this after making object of your class.
这是在你的班级的对象之后。
PlaylistViewController *PLVC = [[PlaylistViewController alloc] init];
PLVC.SongArray=yourAry;
[self.navigationController pushViewController:PLVC animated:YES];
回答by Ravindra Bagale
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 中创建一个 NSMutableArray 属性 secondMutArray。在 firstViewController 中,您要传递 mutablearray 的位置,创建第二个视图控制器的实例并将 self.mutableArray 分配给 secondMutArray。像这样
SecondViewController *secondViewController=[[SecondViewController alloc]init];
secondViewController.secondMutArray=self.mutableArray

![xcode -[UITableView _configureCellForDisplay:forIndexPath:] 中的断言失败](/res/img/loading.gif)