2013-04-22 136 views

回答

8

您可以通過兩種方式分享:

  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; 
  • 使用NSUserDefaults
  •  [[NSUserDefaults standardUserDefaults] setValue:SongArray forKey:@"songArray"]; 
        [[NSUserDefaults standardUserDefaults] synchronize]; 
    
        NSMutableArray *arr = [[NSUserDefaults standardUserDefaults] valueForKey:@"songArray"]; 
    
    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; 
    
    0

    讓您NSMutableArray的財產和合成它。

    而且這是在你的課程成爲對象之後。

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

    在secondViewController中創建一個NSMutableArray屬性secondMutArray。 在firstViewController中,你想傳遞mutablearray,那裏創建第二個viewcontroller的實例&將self.mutableArray賦值給secondMutArray。 這樣

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

    假設你想從其他視圖控制器通過NSMutableArrayPlaylistViewController可以說viewcontroller .M那就不要考慮Controller.m或者以下

    PlaylistViewController *play=[[PlaylistViewController alloc]initwithnibname:@"PlaylistViewController"]; 
    
    play.SongArray=arrayofSongsWhichYouWantToPass; 
    
    [self.navigationController pushViewController:play animated:YES]; 
    
    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 
    } 
    
    相關問題