gpt4 book ai didi

IOS:删除 imageView

转载 作者:行者123 更新时间:2023-11-28 18:43:33 24 4
gpt4 key购买 nike

我有这个代码:

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“fix”。从我的 self.view 中删除所有这些 imageView 的方法是什么?

最佳答案

如果你只想删除 UIImageView 的实例,你可以尝试这样的事情:

for (UIView *v in self.view.subviews) {
if ([v isKindOfClass:[UIImageView class]]) {
[v removeFromSuperview];
}
}

更新:

正如 vikingosegundo 在评论中所写,您可以直接执行此操作。

如果您将每个 ImageView 添加到一个数组中,您可以稍后像这样从 View 中删除它们:

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;
}
}

稍后,将它们从 View 中移除:

for (UIImageView *v in images) {
[v removeFromSuperview];
}

关于IOS:删除 imageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8228043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com