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

Iphone 将NSMutableArray传递给一个视图控制器,再传递给另一个视图控制器,iphone,ios,objective-c,xcode,ios4,Iphone,Ios,Objective C,Xcode,Ios4,我试图在两个视图控制器之间传递NSMutableArray。请让我知道我能为此做些什么 在playliViewController.h文件中 NSMutableArray *SongArray; @property(nonatomic,retain)NSMutableArray *SongArray; 我希望将其传递给另一个视图控制器您可以通过两种方式共享: 使用性质 示例 [[NSUserDefaults standardUserDefaults] setValue:SongArr

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

在playliViewController.h文件中

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

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

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

  • 使用性质
  • 示例

         [[NSUserDefaults standardUserDefaults] setValue:SongArray forKey:@"songArray"];
         [[NSUserDefaults standardUserDefaults] synchronize];
    
         NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"songArray"];
    
    .h文件中

        @interface ABCController : UIViewController
        {
            NSMutableArray *detailArray;
        }
        @property(nonatomic,retain)NSMutableArray *detailArray;
    
        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;
    
    .m文件中

        @interface ABCController : UIViewController
        {
            NSMutableArray *detailArray;
        }
        @property(nonatomic,retain)NSMutableArray *detailArray;
    
        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"];
    

    制作
    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
    从其他一些视图控制器传递到
    playliViewController
    ,让我们假设
    viewcontroller
    .m,然后在视图控制器.m中执行以下操作

    PlaylistViewController *play=[[PlaylistViewController alloc]initwithnibname:@"PlaylistViewController"];
    
    play.SongArray=arrayofSongsWhichYouWantToPass;
    
    [self.navigationController pushViewController:play animated:YES];
    

    您可以将希望将数组传递给的视图控制器设置为原始视图控制器的代理(在您的示例中为播放视图控制器)

    **OriginViewController.h**
    @协议源ViewControllerDelegate{
    -(void)passMutableArray:(NSMutableArray*)数组;
    }
    @接口OriginViewController:UIViewController
    @属性(非原子,保留)id委托;
    @属性(非原子,保留)NSMutableArray*数组;
    **OriginViewController.m**
    //将DestinationViewController设置为OriginViewController的委托(不一定在OriginViewController.m中
    //当您想要传递数组时,只需将消息发送给代理即可
    [self.delegate passMutableArray:array];
    **DestinationViewController.h**
    @接口目标ViewController:UIViewController
    //在m文件中实现协议功能
    **DestinationViewController.m**
    -(void)passMutableArray:(NSMutableArray*)数组{
    //对数组执行任何操作
    }