Pass NSMutableArray to one view controller to another

前端 未结 6 1310
-上瘾入骨i
-上瘾入骨i 2020-12-22 13:49

I am trying to pass a NSMutableArray between two view controller. Please let me know what can i do for this

In the PlaylistViewController.h file I have



        
相关标签:
6条回答
  • 2020-12-22 14:22

    Suppose you want to pass NSMutableArray to PlaylistViewController from some other view controller lets say viewcontroller.m then do following in view controller.m

    PlaylistViewController *play=[[PlaylistViewController alloc]initwithnibname:@"PlaylistViewController"];
    
    play.SongArray=arrayofSongsWhichYouWantToPass;
    
    [self.navigationController pushViewController:play animated:YES];
    
    0 讨论(0)
  • 2020-12-22 14:27
    **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;
    
    0 讨论(0)
  • 2020-12-22 14:29

    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 *secondViewController=[[SecondViewController alloc]init];
    secondViewController.secondMutArray=self.mutableArray
    
    0 讨论(0)
  • 2020-12-22 14:33

    You can share in two ways:

    1. Using property

    Example

    In .h file

        @interface ABCController : UIViewController
        {
            NSMutableArray *detailArray;
        }
        @property(nonatomic,retain)NSMutableArray *detailArray;
    

    In .m file

        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. Using NSUserDefaults

    Example

         [[NSUserDefaults standardUserDefaults] setValue:SongArray forKey:@"songArray"];
         [[NSUserDefaults standardUserDefaults] synchronize];
    
         NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"songArray"];
    
    0 讨论(0)
  • 2020-12-22 14:33

    You could set view controller you wish to pass array to as a delegate of origin view controller (in your case 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
    }
    
    0 讨论(0)
  • 2020-12-22 14:44

    Make property of your NSMutableArray and synthesize it.

    And this after making object of your class.

    PlaylistViewController *PLVC = [[PlaylistViewController alloc] init];
    PLVC.SongArray=yourAry;
    [self.navigationController pushViewController:PLVC animated:YES];
    
    0 讨论(0)
提交回复
热议问题