我在 "HeroListViewController" 中创建了 NSMutale 数组。我想在另一个 viewController 中使用它,即 MapTutorialViewController。我试过这样。
在 HeroListViewController.h 中
MapTutorialViewController *maptutorialcontroller;
NSMutableArray *listData;
设置属性并正确合成
在 HeroListViewController.m 中
- (void)viewDidLoad {
[super viewDidLoad];
listData = [[NSMutableArray alloc] init];
}
- (UITableViewCell *)tableViewUITableView *)theTableView cellForRowAtIndexPathNSIndexPath *)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/
欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) | Powered by Discuz! X3.4 |