我有一个视图控制器PlayerViewController,我试图将一个NSMutableArray:stories传递给视图控制器PlaylistViewController
在PlayerViewController.h文件中我有
@class PlaylistViewController;
在PlayerViewController.m文件中我有
//stories is a NSMutableArray that does have content in it
PlaylistViewController *sVC = [[PlaylistViewController alloc] initWithNibName:@"PlaylistViewController" bundle:nil];
sVC.playSongArray = [[NSMutableArray alloc]init];
sVC.playSongArray = stories;
在PlaylistViewController.h文件中我有
NSMutableArray *playSongArray;
和
@property(nonatomic,retain)NSMutableArray *playSongArray;
我也在.m文件中合成它
但是当我运行代码时,PlaylistViewController中的playSongArray为空。我做错了什么?
答案 0 :(得分:1)
您是如何呈现此观点的?我已经多次看到我必须在设置这样的属性之前呈现视图才能正确应用它们。在呈现/显示视图并且初始化过程的一部分将属性设置为nil
之前,视图似乎未完全初始化。
假设您以模态方式呈现此内容,请尝试以下顺序。
//stories is a NSMutableArray that does have content in it
PlaylistViewController *sVC = [[PlaylistViewController alloc] initWithNibName:@"PlaylistViewController" bundle:nil];
[self presentModalViewController:svc animated:YES]; // if modal
[self pushViewController:svc animated:YES]; // if part of a uinavigationcontroller
sVC.playSongArray = [[NSMutableArray alloc]init];
sVC.playSongArray = stories;
答案 1 :(得分:0)
而不是:
sVC.playSongArray = [[NSMutableArray alloc]init];
sVC.playSongArray = stories;
尝试
sVC.playSongArray = [[NSMutableArray alloc]initWithArray:stories];
如果它无效,请在PlaylistViewController
控制器中,在playSongArray = [[NSMutableArray alloc]init];
函数中添加initWithNibName
,然后创建一个函数:
- (void)setPlaySongs:(NSArray *)songs {
[playSongArray addObjectsFromArray:songs];
}
并以这种方式加载视图控制器:
PlaylistViewController *sVC = [[PlaylistViewController alloc] initWithNibName:@"PlaylistViewController" bundle:nil];
[sVC setPlaySongs:stories];
答案 2 :(得分:0)
我怀疑你过早检查playSongArray
的价值。您的PlaylistViewController应如下所示:
@interface PlaylistViewContror {
NSMutableArray *playSongArray;
}
- (id)initWithPlaySongArray:(NSMutableArray *)array;
@property(nonatomic,retain)NSMutableArray *playSongArray;
@end
@implementation PlaylistViewContror
@synthesize playSongArray;
- (id)initWithPlaySongArray:(NSMutableArray *)array
{
if (!(self = [super initWithNibName:@"PlaylistViewController" bundle:nil]))
return nil;
playSongArray = array;
return self;
}
- (void)awakeFromNib
{
// do things with playSongArray here
}
@end
然后在你的PlayerViewController中,你只需:
//stories is a NSMutableArray that does have content in it
PlaylistViewController *sVC = [[PlaylistViewController alloc] initWithPlaySongArray:stories];