【发布时间】:2013-05-20 18:19:09
【问题描述】:
我试图通过 prepareWithSegue 传递一个数组,但是当我启动应用程序时我得到了 null
这是代码:
-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([segue.destinationViewController isEqual:@"table"]) {
PersonsViewController *person= [[PersonsViewController alloc]init];
[person setAnArray:anArray];
person = segue.destinationViewController;
}
}
这是 setAnArray 方法:
-(void)setAnArray:(NSMutableArray *)anArray
{
array = [[NSMutableArray alloc]initWithArray:anArray];
if (array != nil) {
NSLog(@"array is copied !!");
}
}
数据应该从 viewController(嵌入 UINavigation 控制器)传递到 PersonViewController(这是 tableview)并且表格上没有任何显示,所以我 NSLogged 数组计数并发现它为零,所以我用这段代码做了一些进一步的检查:
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
#warning Incomplete method implementation.
// Return the number of rows in the section.
if (array == nil) {
NSLog(@"array is null");
}
else
{
NSLog(@"array count is %lu",(unsigned long)[array count]);
return [array count];
}
我得到 array is null 消息。
请帮我解决这个问题
【问题讨论】:
-
这一行 person = segue.destinationViewController 写完了你分配了几行的人。 segue.destinationViewController 是 PersonsViewController 吗?
-
@andrewlattis 绝对是这样。接得好。是的,您不会在
prepareForSegue中实例化 (alloc/init) 目标视图控制器。它已经为您实例化了。安德鲁,如果你把它作为答案发布,我会投赞成票!