xcode IOS:删除图像视图
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8228043/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IOS: remove imageView
提问by cyclingIsBetter
I have this code:
我有这个代码:
for(Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
return;
}
}
in this code I add an imageView on self.view but in my app i call this "for" many times and finally I have my self.view with 4 or 5 imageView "fix". What's the way to remove all these imageView from my self.view?
在这段代码中,我在 self.view 上添加了一个 imageView,但在我的应用程序中,我多次称其为“for”,最后我的 self.view 带有 4 或 5 个 imageView“修复”。从我的 self.view 中删除所有这些 imageView 的方法是什么?
回答by matsr
If you only want to remove instances of UIImageView, you can try something like this:
如果你只想删除 UIImageView 的实例,你可以尝试这样的事情:
for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
[v removeFromSuperview];
}
}
Update:
更新:
As vikingosegundowrote in the comments, you can do this instad.
正如vikingosegundo在评论中所写,您可以立即执行此操作。
If you add each imageview to an array, you can remove them from the view later on like this:
如果将每个图像视图添加到数组中,您可以稍后从视图中删除它们,如下所示:
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;
}
}
The later on, remove them from the view:
稍后,将它们从视图中删除:
for (UIImageView *v in images) {
[v removeFromSuperview];
}
回答by vikingosegundo
NSMutableArray *images = [NSMutableArray array];
for Contact *contact in myArray){
if(...){
UIImageView *fix = [[UIImageView alloc] initWithImage:myImage];
[self.view addSubview:fix];
[fix setFrame:[contact square]];
[images addObject:fix];
}
}
for (UIView *v in images){
[v removeFromSuperview];
}
Another approach
另一种方法
for(UIView *v in self.view.subviews)
if([v isKindOfClass:[UIImageView class]])
[v removeFromSuperview];
I put an exampletogether.
回答by lluismontero
Just call removeFromSuperview for every subView. Something like:
只需为每个子视图调用 removeFromSuperview。就像是:
for(UIView *subview in self.view.subviews)
[subview removeFromSuperview];