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

时间:2013-04-22 07:58:15

标签: iphone ios objective-c xcode ios4

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

在PlaylistViewController.h文件中我有

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

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

6 个答案:

答案 0 :(得分:7)

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

  1. 使用属性
  2. 示例

    .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
    2. 示例

           [[NSUserDefaults standardUserDefaults] setValue:SongArray forKey:@"songArray"];
           [[NSUserDefaults standardUserDefaults] synchronize];
      
           NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"songArray"];
      

答案 1 :(得分: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;

答案 2 :(得分:1)

假设您要从其他视图控制器传递NSMutableArrayPlaylistViewController,请说viewcontroller。m然后在视图controller.m中执行以下操作

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

play.SongArray=arrayofSongsWhichYouWantToPass;

[self.navigationController pushViewController:play animated:YES];

答案 3 :(得分: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
}

答案 4 :(得分:0)

制作NSMutableArray的财产并合成它。

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

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

答案 5 :(得分:0)

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

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