objective-c - 如何将 NSMutableArray 传递给另一个 ViewController 类
<p><p>我在 <strong>"HeroListViewController"</strong> 中创建了 NSMutale 数组。我想在另一个 viewController 中使用它,即 <strong>MapTutorialViewController</strong>。我试过这样。 </p>
<p>在 HeroListViewController.h 中</p>
<pre><code>MapTutorialViewController *maptutorialcontroller;
NSMutableArray *listData;
</code></pre>
<p>设置属性并正确合成</p>
<p>在 HeroListViewController.m 中</p>
<pre><code>- (void)viewDidLoad {
;
listData = [ init];
}
- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *HeroTableViewCell = @"HeroTableViewCell";
UITableViewCell *cell = ;
if (cell == nil) {
cell = [[ initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:HeroTableViewCell] autorelease];
}
NSManagedObject *oneHero = ;
NSInteger tab = ;
switch (tab) {
case kByName:
cell.textLabel.text = ;
cell.detailTextLabel.text = ;
break;
case kBySecretIdentity:
cell.detailTextLabel.text = ;
cell.textLabel.text = ;
default:
break;
}
];
count=;
printf("No of items of listData:%u\n", count);
if(maptutorialcontroller==nil){
maptutorialcontroller= [initWithNibName:@"MapTutorialViewController" bundle:nil];
maptutorialcontroller.secondarray=listData;
}
count=;
printf("No of items of seconarray :%u\n", count);
return cell;
</code></pre>
<p>}</p>
<p><strong>输出:</strong> listData 的项目数:3
seconarray 的项目数:3//两者都是正确的</p>
<p>但是我遇到的问题是,当我尝试像这样在“MapTutorialViewController”中使用第二个数组时,
在 MapTutorialViewController.h 中</p>
<pre><code> HeroListViewController *heroviewcontroller;
NSMutableArray *secondarray;
</code></pre>
<p>设置属性并正确合成</p>
<p>在 MapTutorialViewController.m 中</p>
<pre><code> - (void)viewDidLoad
{
heroviewcontroller = [initWithNibName:@"HeroListViewController"bundle:nil];
self.secondarray=;
//secondarray= heroviewcontroller.listData;
int count;
count = ;
//
printf("No of items of secondarray from MapTutorialViewContriller :%u\n", count);
}
</code></pre>
<p>输出:来自 MapTutorialViewContriller 的 secondarray 的项目数:0<br/>
<strong>为什么是 0</strong></p>
<p>我的代码有什么问题,请帮助我</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p><strong>示例</strong></p>
<pre><code>firstviewcontroller .h file
before @interface
use @class secondViewcontroller;
declare this inside of @interfacewith
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.
</code></pre></p>
<p style="font-size: 20px;">关于objective-c - 如何将 NSMutableArray 传递给另一个 ViewController 类,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/6522141/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/6522141/
</a>
</p>
页:
[1]