我在“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
什么错我的代码,请帮我
它不能像上面那样完成,因为当你将init分配给viewcontroller时,只有你在HeroListViewController的init函数中初始化它们 – user784625