ユーザーが画面上のポイントに触れたときに画面に画像を表示する一時変数名 imgView を保持する imgBall という名前の NSArray があります。
- (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をスローせずにこれを行うにはどうすればよいですか?