NSMutableArray objectatindex总是显示最后一个对象
问题描述:
我的问题是我的NSMutableArray
总是得到objectatindex
-方法的最后一个元素。NSMutableArray objectatindex总是显示最后一个对象
我有一个数组,派生自UIViewController
。我想展示一个接一个的视图。
首先我填充数组:后
ContactViewController *ContactView = [[ContactViewController alloc]init];
QuestionViewController *QuesView = [[QuestionViewController alloc]init];;
ContactView.mydelegate = self;
[QuesView setDelegate:self];
[Views addObject:ContactView];
Views =[[NSMutableArray alloc]initWithCapacity:11];
for (int i = 0; i < 11; i++) {
[QuesView setParam:@"text" :i ];
[Views addObject:QuesView];
}
我希望得到实际的视图,并跳转到下一个这样的:
-(IBAction)Button:(id)sender
{
UIViewController *newView = [[UIViewController alloc]init];
newView = (UIViewController*)[Views objectAtIndex:enumerator];
[self presentViewController:newView animated:YES completion: nil];
enumerator++;
//dismiss old view here....
NSLog(@"number %d",[newView getNumber]);
}
新视图没有显示和数字在日志中始终是数组的最后一个数字。我试图在“视图”中使用for循环遍历所有元素,但每个对象中始终存在最后一个数字....
任何提示?
答
您可能想要创建多个QuestionViewController实例。但是你实际上只创建了一个对象,并且在其上调用了setParam 11次。
此行[Views addObject:ContactView]
也没有效果,因为您创建了一个新的数组对象并将其分配给下一行中的Views。与UIViewController *newView = [[UIViewController alloc]init]
同样的事情。希望你使用ARC,否则会造成内存泄漏!
非常感谢!你是绝对正确的!我正在使用ARC ... – Phil