I have an NSArray named imgBall that holds a temporary variable name imgView, which displays an image on the screen when the user touches a point on the screen.
- (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];
}
The user can create multiple instances by touching anywhere on the screen. Could mean 5, 10, or 20 different balls in the array.
Now, i have a button that needs to ‘clear’ the screen and remove all instances of imgView.
I have tried the following:
for (UIImageView *imgView in imgBall) {
[self.view removeFromSuperview:imgView];
}
and
for (UIImageView *imgView in imgBall) {
[imgBall removeObject:imgView];
}
But they both yield SIGABRT and throw the exception:
Terminating app due to uncaught exception 'NSGenericException', reason:
'*** Collection <__NSArrayM: 0x735f4a0> was mutated while being enumerated.'
What way can i do this without getting SIGABRT thrown every time?
I think you want:
You also need to do [imgView release] after [imgBall addObject:imgView] when you create it in touchesEnded, otherwise you will leak memory.