-3

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

在 PlaylistViewController.h 文件中我有

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

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

4

6 回答 6

9

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

  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"];
于 2013-04-22T08:03:03.377 回答
7
**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;
于 2013-04-22T08:03:24.043 回答
1

假设您想从其他视图控制器传递NSMutableArray给.m 然后在视图控制器.m 中执行以下操作PlaylistViewControllerviewcontroller

PlaylistViewController *play=[[PlaylistViewController alloc]initwithnibname:@"PlaylistViewController"];

play.SongArray=arrayofSongsWhichYouWantToPass;

[self.navigationController pushViewController:play animated:YES];
于 2013-04-22T08:09:28.853 回答
1

您可以将希望将数组传递给的视图控制器设置为原始视图控制器的代表(在您的情况下为 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
}
于 2013-04-22T08:17:53.150 回答
0

在 secondViewController 中创建一个 NSMutableArray 属性 secondMutArray。在要传递 mutablearray 的 firstViewController 中,创建第二个 viewcontroller 的实例并将 self.mutableArray 分配给 secondMutArray。像这样

SecondViewController *secondViewController=[[SecondViewController alloc]init];
secondViewController.secondMutArray=self.mutableArray
于 2013-04-22T08:06:05.563 回答
0

制作你的财产NSMutableArray并合成它。

这在成为你的班级的对象之后。

PlaylistViewController *PLVC = [[PlaylistViewController alloc] init];
PLVC.SongArray=yourAry;
[self.navigationController pushViewController:PLVC animated:YES];
于 2013-04-22T08:03:57.323 回答