Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 是否删除基于标记的UIView子视图?_Iphone_Objective C_Ipad - Fatal编程技术网

Iphone 是否删除基于标记的UIView子视图?

Iphone 是否删除基于标记的UIView子视图?,iphone,objective-c,ipad,Iphone,Objective C,Ipad,我正在创建这样一个视图: 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

我正在创建这样一个视图:

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];
这可能会在此视图控制器中发生多次,因此在创建这样的新视图之前,我想删除可能存在于此标记中的任何视图,我正在尝试以下操作:

UIView *removeView  = [self.view viewWithTag:999];
[removeView removeFromSuperview];
不知什么原因,这不起作用,有人看到我的问题了吗


我想我可以循环浏览所有视图并检查标记,但更希望有一个更优雅、更直接的解决方案。

问题是您可能只删除几个视图中的一个视图?试试这个:

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

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

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