iOS:删除imageView

发布于 2024-12-17 13:49:04 字数 437 浏览 0 评论 0原文

我有这段代码:

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 的方法是什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

阳光的暖冬 2024-12-24 13:49:04

如果您只想删除 UIImageView 的实例,您可以尝试如下操作:

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

更新:

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

如果将每个图像视图添加到数组中,则可以稍后将它们从视图中删除,如下所示:

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

If you only want to remove instances of UIImageView, you can try something like this:

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

Update:

As vikingosegundo wrote in the comments, you can do this instad.

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];
}
此生挚爱伱 2024-12-24 13:49:04

只需为每个子视图调用removeFromSuperview即可。像这样的东西:

for(UIView *subview in self.view.subviews)
    [subview removeFromSuperview];

Just call removeFromSuperview for every subView. Something like:

for(UIView *subview in self.view.subviews)
    [subview removeFromSuperview];
日记撕了你也走了 2024-12-24 13:49:04
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];
}

另一种方法是

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

我将示例放在一起。

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 example together.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文