如何将NSMutableArray传递给另一个ViewContr

如何将NSMutableArray传递给另一个ViewContr

本文介绍了如何将NSMutableArray传递给另一个ViewController类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have created NSMutale Array in "HeroListViewController". I want use it in another viewController which is MapTutorialViewController. I tried like this.

in HeroListViewController.h

in HeroListViewController.h

MapTutorialViewController *maptutorialcontroller;
NSMutableArray *listData;

设置属性并正确合成它们

set properties and synthesize them correctly

in HeroListViewController.m

in 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 //两者都正确

OUTPUTS : No of items of listData:3 No of items of seconarray :3 // both are correct

但我遇到的问题是什么时候我尝试在这样的MapTutorialViewController中使用secondarray,在MapTutorialViewController.h中使用

BUT the the problem I have, when I try to use the secondarray in "MapTutorialViewController" like this,in MapTutorialViewController.h

    HeroListViewController *heroviewcontroller;
  NSMutableArray *secondarray;

设置属性并正确合成它们

set properties and synthesize them correctly

在MapTutorialViewController.m中

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

输出:MapTutorialViewContriller中没有第二个数组的项目:0

为什么是0

OUTPUT : No of items of secondarray from MapTutorialViewContriller :0
Why it is 0

我的代码出了什么问题,请帮帮我

whats the wrong with my code, please help me

推荐答案

示例

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.

这篇关于如何将NSMutableArray传递给另一个ViewController类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 11:12