gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 10:18:46 25 4
gpt4 key购买 nike

我在 "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 的 secondarray 的项目数: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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com