我试图通过prepareWithSegue一个数组,但我得到空当我启动应用程序传递一个数组prepareforsegue
这是代码:
-(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];
}
和我得到数组为空消息。
请帮我解决这个
这一行的人= segue.destinationViewController是在写你分配一些生产线的达人。是segue.destinationViewController PersonsViewController? –
@andrewlattis这绝对是。接得好。是的,你不要在'prepareForSegue'中实例化('alloc' /'init')目标视图控制器。它已经为你实例化了。安德鲁,如果你张贴这个答案,我会投票! – Rob