我已经在“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”中使用secondarray时,我遇到了问题,
在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/