我在「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