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

标签 objective-c ios iphone-sdk-3.0 uiviewcontroller nsmutablearray

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

在 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”中使用第二个数组时, 在MapTutorialViewController.h

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

输出:MapTutorialViewContriller 中第二个数组的项目数: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.

关于objective-c - 如何将 NSMutableArray 传递给另一个 ViewController 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6522141/

相关文章:

ios - 如何从NSString中删除带前缀的零

objective-c - Xcode 链接失败消息 - 这是什么意思?

ios - 右上角的 swift 4 中的 byRoundingcorners 不起作用

ios - 将 View Controller 导入另一个 Controller 以进行 segueing 是否安全?

ios - 用于像 Facebook 一样在墙上发帖的 Google plus API

iphone - 如何使用 iPhone SDK 3.0 中的 UISearchDisplayController 实现始终位于顶部的搜索栏

objective-c - 堆栈 - 框架 - Objective-C 中的堆

ios - 使用 unwind segue 将数据发送回之前的 Controller ?

iPhone UILabel 文字软阴影

iphone - 访问和使用移动WiFi.framework