IOS:删除imageView
我有以下代码:IOS:删除imageView,ios,xcode,uiimageview,Ios,Xcode,Uiimageview,我有以下代码: for(Contact *contact in myArray){ if(...){ UIImageView *fix = [[UIImageView alloc] initWithImage:myImage]; [self.view addSubview:fix]; [fix setFrame:[contact square]]; return; }
for(Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
return;
}
}
在这段代码中,我在self.view上添加了一个imageView,但在我的应用程序中,我多次将其称为“for”,最后,我将我的self.view与4或5个imageView一起“修复”。
如何从我的self.view中删除所有这些imageView 只需为每个子视图调用removeFromSuperview。比如:
for(UIView *subview in self.view.subviews)
[subview removeFromSuperview];
只需为每个子视图调用removeFromSuperview。比如:
for(UIView *subview in self.view.subviews)
[subview removeFromSuperview];
如果您只想删除UIImageView的实例,可以尝试以下操作:
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
[v removeFromSuperview];
}
}
NSMutableArray *images = [[NSMutableArray alloc] init];
for Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
[images addObject:fix]; // Add the image to the array.
return;
}
}
更新:
正如vikingosegundo在评论中所写,您可以使用instad
如果将每个imageview添加到阵列中,则可以稍后将其从视图中删除,如下所示:
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
[v removeFromSuperview];
}
}
NSMutableArray *images = [[NSMutableArray alloc] init];
for Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
[images addObject:fix]; // Add the image to the array.
return;
}
}
稍后,将它们从视图中删除:
for (UIImageView *v in images) {
[v removeFromSuperview];
}
如果您只想删除UIImageView的实例,可以尝试以下操作:
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
[v removeFromSuperview];
}
}
NSMutableArray *images = [[NSMutableArray alloc] init];
for Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
[images addObject:fix]; // Add the image to the array.
return;
}
}
更新:
正如vikingosegundo在评论中所写,您可以使用instad
如果将每个imageview添加到阵列中,则可以稍后将其从视图中删除,如下所示:
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
[v removeFromSuperview];
}
}
NSMutableArray *images = [[NSMutableArray alloc] init];
for Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
[images addObject:fix]; // Add the image to the array.
return;
}
}
稍后,将它们从视图中删除:
for (UIImageView *v in images) {
[v removeFromSuperview];
}
另一种方法
for(UIView *v in self.view.subviews)
if([v isKindOfClass:[UIImageView class]])
[v removeFromSuperview];
我把一本书放在一起
另一种方法
for(UIView *v in self.view.subviews)
if([v isKindOfClass:[UIImageView class]])
[v removeFromSuperview];
我把一个文件放在一起。这样它就删除了我所有的imageView,但我应该只删除我在“for”中添加的那些文件;我可以使用NSMutableArray吗?您不能直接将UIImageView添加到数组中,然后通过从数组中引用将其从self.view中删除。您需要每个UIImageView的引用(即标记)。我更新了我的答案,我希望这对你有用。这不会在数组中添加数字,而只是简单的UIImageViews
[images addObject:fix]
对于(图像中的UIView*v){[v removeFromSuperview];}
同样,return语句很可能不是您想要的。这将返回整个方法。这样,它将删除我的所有imageView,但我应该只删除我在“for”中添加的那些;我可以使用NSMutableArray吗?您不能直接将UIImageView添加到数组中,然后通过从数组中引用将其从self.view中删除。您需要每个UIImageView的引用(即标记)。我更新了我的答案,我希望这对你有用。这不会在数组中添加数字,而只是简单的UIImageViews[images addObject:fix]
对于(图像中的UIView*v){[v removeFromSuperview];}
同样,return语句很可能不是您想要的。这将返回整个方法。return语句将返回方法。return语句将返回方法。