我有一个名为 imgBall 的 NSArray,它包含一个名为 imgView 的临时变量,当用户触摸屏幕上的某个点时,它会在屏幕上显示一个图像。
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)];
imgView.image = [UIImage imageNamed:@"ball.png"];
[self.view addSubview:imgView];
imgView.center = [myTouch locationInView:self.view];
[imgBall addObject:imgView];
}
用户可以通过触摸屏幕上的任意位置来创建多个实例。可能意味着阵列中有 5、10 或 20 个不同的球。
现在,我有一个需要“清除”屏幕并删除所有 imgView 实例的按钮。我尝试了以下方法:
for (UIImageView *imgView in imgBall) {
[self.view removeFromSuperview:imgView];
}
和
for (UIImageView *imgView in imgBall) {
[imgBall removeObject:imgView];
}
但它们都产生 SIGABRT 并抛出异常:
Terminating app due to uncaught exception 'NSGenericException', reason:
'*** Collection <__NSArrayM: 0x735f4a0> was mutated while being enumerated.'
我有什么方法可以做到这一点而不会每次都抛出 SIGABRT?