Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/38.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/111.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone iOS:是否删除NSArray创建的UIImageView实例?_Iphone_Ios_Sdk_Nsarray_Sigabrt - Fatal编程技术网

Iphone iOS:是否删除NSArray创建的UIImageView实例?

Iphone iOS:是否删除NSArray创建的UIImageView实例?,iphone,ios,sdk,nsarray,sigabrt,Iphone,Ios,Sdk,Nsarray,Sigabrt,我有一个名为imgBall的NSArray,其中包含一个临时变量名imgView,当用户触摸屏幕上的某个点时,它会在屏幕上显示一个图像 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(40, 40, 40, 40)]; imgView.image = [UI

我有一个名为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]; 
}

但他们都放弃了SIGABRT并抛出了例外:

Terminating app due to uncaught exception 'NSGenericException', reason: 
'*** Collection     <__NSArrayM: 0x735f4a0> was mutated while being enumerated.'
由于未捕获的异常“NSGenericeException”而终止应用程序,原因:
“***集合在枚举时发生了变异。”
我怎么做才能不让SIGABRT每次都被抛出?

我想你想要:

for (UIImageView *imgView in imgBall) {
    [imgView removeFromSuperview]; 
    [imgBall removeObject:imgView];
}
当您在touchesEnded中创建[imgBall addObject:imgView]后,还需要执行[imgView release],否则会泄漏内存。

我想您需要:

for (UIImageView *imgView in imgBall) {
    [imgView removeFromSuperview]; 
    [imgBall removeObject:imgView];
}
在touchesEnded中创建[imgBall addObject:imgView]时,还需要在[imgBall addObject:imgView]之后执行[imgView release],否则会泄漏内存。

这样做:

for (UIImageView *imgView in imgBall)
{
[imgView removeFromSuperview];
}
然后不要忘记释放并将数组imgBall设置为nil,以避免内存问题。如果它是一个NSMutableArray,只需调用RemoveAllObject。

这样做:

for (UIImageView *imgView in imgBall)
{
[imgView removeFromSuperview];
}

然后不要忘记释放并将数组imgBall设置为nil,以避免内存问题。如果是NSMutableArray,只需调用RemoveAllObject。

您应该像

for (UIImageView *imgView in imgBall) {
    [imgView removeFromSuperview];
}
[imgBall removeAllObjects];

你应该像这样做

for (UIImageView *imgView in imgBall) {
    [imgView removeFromSuperview];
}
[imgBall removeAllObjects];

@nworksdev在删除所有UIImageView之后,您可能需要调用
[imgBall removeAllObjects]
。明白了,我正要问一下。谢谢大家!@nworksdev在删除所有UIImageView之后,您可能需要调用
[imgBall removeAllObjects]
。明白了,我正要问一下。非常感谢。别忘了接受答案,点击复选标记=D选择您喜欢的答案,当然我认为您可以选择
[imgView removeFromSuperview]别忘了接受答案单击复选标记=D选择您喜欢的答案,当然我认为您选择了
[imgView removeFromSuperview]