如何将一个NSMutableArray传递给另一个ViewController类

我在“HeroListViewController”中创build了NSMutale数组。 我想在另一个是MapTutorialViewController的viewController中使用它。 我试过这样

在HeroListViewController.h中

MapTutorialViewController *maptutorialcontroller; NSMutableArray *listData; 

设置属性并正确合成它们

在HeroListViewController.m中

 - (void)viewDidLoad { [super viewDidLoad]; listData = [[NSMutableArray alloc] init]; } - (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *HeroTableViewCell = @"HeroTableViewCell"; UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:HeroTableViewCell]; if (cell == nil) { cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:HeroTableViewCell] autorelease]; } NSManagedObject *oneHero = [self.fetchedResultsController objectAtIndexPath:indexPath]; NSInteger tab = [tabBar.items indexOfObject:tabBar.selectedItem]; switch (tab) { case kByName: cell.textLabel.text = [oneHero valueForKey:@"name"]; cell.detailTextLabel.text = [oneHero valueForKey:@"secretIdentity"]; break; case kBySecretIdentity: cell.detailTextLabel.text = [oneHero valueForKey:@"name"]; cell.textLabel.text = [oneHero valueForKey:@"secretIdentity"]; default: break; } [listData addObject: [oneHero valueForKey:@"secretIdentity"]]; count=[listData count]; printf("No of items of listData:%u\n", count); if(maptutorialcontroller==nil){ maptutorialcontroller= [[MapTutorialViewController alloc]initWithNibName:@"MapTutorialViewController" bundle:nil]; maptutorialcontroller.secondarray=listData; } count=[maptutorialcontroller.secondarray count]; printf("No of items of seconarray :%u\n", count); return cell; 

}

OUTPUTS: listData的项目数量:3 seconarray的项目数目:3 //两者都是正确的

但是我有这个问题,当我尝试使用MapTutorialViewController.h中的“MapTutorialViewController”中的第二个数组时

  HeroListViewController *heroviewcontroller; NSMutableArray *secondarray; 

设置属性并正确合成它们

在MapTutorialViewController.m中

  - (void)viewDidLoad { heroviewcontroller = [[HeroListViewController alloc]initWithNibName:@"HeroListViewController" bundle:nil]; self.secondarray=[heroviewcontroller.listData mutableCopy]; //secondarray= heroviewcontroller.listData; int count; count = [secondarray count]; // printf("No of items of secondarray from MapTutorialViewContriller :%u\n", count); } 

OUTPUT:来自MapTutorialViewContriller的第二arrays的项目数量:0
为什么是0

我的代码有什么问题,请帮助我

 firstviewcontroller .h file before @interface use @class secondViewcontroller; declare this inside of @interface with secondViewcontroller *sVC; then in firstViewController.m file before @implementation #import "secondViewcontroller.h" then ------------------- secondVC.h file @interface inside declare this say NSMutableArray *secondarray; and sythasize them. ------------------- after this in firstViewcontroller.h viewdidload create this sVC by alloc and initwithnibname then sVC.secondArray=urfirstArray; now while u push this sVC controller to navigation controller u can nslog this array in viewdidload. 

这只会在你创build并在init方法中填充可变数组的时候才起作用。

你应该考虑授权和/或通知。

这个数组是如何在HeroListViewController中创build的? 在这个方法中,您正在创build一个HeroListViewController的新实例,并试图从中获取一个属性。 如果你已经在内存中有一个HeroListViewController,这是完全错误的。

在这个viewDidLoad方法的类上做一个属性。 它应该是NSMutableArraytypes的。 当你分配和初始化这个类时,从HeroListViewController上调用[set myArray:heroListArray]。 这应该让你访问它。

我假设你有一个包含这个新视图和英雄列表视图的视图。 如果是这样的话,那么你可以在新视图中创build一个属性,如下所示:

 @property (nonatomic,retain)HeroListViewController *heroListViewController; 

然后将其设置为等于来自外部的heroList:

 newView.heroListViewController = HeroListViewController; 

目前你的代码的主要问题是,你正在使用alloc init创build一个HeroListViewController的新实例,而你没有访问同样的东西。 通过设置新视图的heroListViewController属性,可以访问正确的viewController。

最后,在新视图的viewDidLoad – 我实际上把代码放在viewWillAppear:(布尔)animation – 你可以把代码来匹配数组。

请注意,这样做的整个方法是混乱的,如果您需要访问多个地方的数组,可以用单例类更好地完成。 上面的内容可以帮助你快速的工作,但是如果你想要一个非常干净的修补程序,请到这里: http : //www.iphonedevsdk.com/forum/iphone-sdk-tutorials/24135-singleton-classes.html