2

我在“HeroListViewController”中创建了NSMutale数组。我想在另一个viewController中使用它,它是MapTutorialViewController。我试过这样。如何将一个NSMutableArray传递给另一个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; 

}

输出:没有的ListData的项目:3 没有seconarray的项目:3 //都是正确

但这个问题我有,当我尝试在MapTutorialViewController.h

HeroListViewController *heroviewcontroller; 
    NSMutableArray *secondarray; 

使用secondarray在“MapTutorialViewController”像这样, 设置属性和正确合成它们

在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); 
    } 

OUT PUT:没有secondarray的项目从MapTutorialViewContriller:0
为什么是0

什么错我的代码,请帮我

+0

它不能像上面那样完成,因为当你将init分配给viewcontroller时,只有你在HeroListViewController的init函数中初始化它们 – user784625

回答

2

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. 
+0

请格式化代码。谢谢。 –

+0

ok.sure .......... thanku –

+0

@madhu,非常感谢您的帮助。我会试试看。 – sajaz

0

这怎么阵列被内HeroListViewController产生的?在这种方法中,您正在创建一个HeroListViewController的新实例并尝试从中获取属性。如果你已经在内存中有一个HeroListViewController,这是完全错误的。

为此viewDidLoad方法创建类的属性。它应该是NSMutableArray类型。当你分配和初始化这个类时,从HeroListViewController调用它[set myArray:heroListArray]。这应该让你访问它。

+0

我像这样创建了NSMutableArray,并设置了属性。 ** NSMutableArray * listData; ** ** @ property(nonatomic,retain)NSMutableArray * listData; **请给我一些代码帮助。因为我对iPhone的开发很陌生。高度欣赏它。 。 。 – sajaz

1

这只有在您创建并在init方法中填充可变数组时才有效。

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

+0

@ dasdom,你可以给我一些代码帮助。因为我对iPhone的开发很陌生。高度欣赏它。 。 。 – sajaz

0

我假设你有一个包含这个新的观点和主人公列表视图的视图。如果是这样的话,那么你可以创建新的视图属性,像这样:

@property (nonatomic,retain)HeroListViewController *heroListViewController; 

,然后将其设置为从外部heroList:

newView.heroListViewController = HeroListViewController; 

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

最后,在新视图的viewDidLoad中 - 我居然把在viewWillAppear中的代码:(BOOL)动画 - 你可以把代码相匹配的阵列。

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

相关问题