删除基于标记的UIView子视图? [英] remove UIView subview based on tag?

查看:96
本文介绍了删除基于标记的UIView子视图?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建这样的视图:

I am creating a view like this:

UILabel *qty = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
qty.backgroundColor = [UIColor whiteColor];
qty.text =[NSString stringWithFormat:@" Qty: %@", currentQty];
qty.alpha = 0.5;
[qty setTag:999];
[self.view addSubview:qty];
[qty release];

这可能会在此视图控制器中多次发生,所以在我创建这样的新视图之前我想要删除此标记可能存在的任何内容,我正在尝试:

This can happen multiple times in this view controller so before I create a new view like this I want to remove any that might exist with this tag, I am trying this:

UIView *removeView  = [self.view viewWithTag:999];
[removeView removeFromSuperview];

由于某种原因这不起作用,有人在这里看到我的问题吗?

This is not working for some reason, anyone see my problem here?

我想我可以遍历所有视图并检查标签,但宁愿有更优雅和直接的解决方案。

I guess i could loop through all the views and check the tag but would rather have a more elegant and direct solution.

推荐答案

问题是你可能只删除了几个视图吗?试试这个:

Is the issue that you're possibly only removing one view of several? Try this:

UIView *removeView;
while((removeView = [self.view viewWithTag:999]) != nil) {
    [removeView removeFromSuperview];
}

如果只有一个视图被创建/标记/删除,你也可以考虑只是添加一个属性来跟踪该视图,并写下:

If there's only one view that's getting created/tagged/removed, you also might consider just adding a property to track that view, and writing:

[self.subView removeFromSuperview];
self.subView = qty;

这篇关于删除基于标记的UIView子视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆