-1

I am trying to remove the UILabel subview from button, through this code but this code removes all subviews from button even its current image. I don't understand that how can i specify only UILabel to remove from button.

if (![[UIImage imageNamed:@"box1.png"] isEqual:button.currentImage]) {
     [button.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
}
4

2 回答 2

2

案例 1:您添加了标签。在这种情况下,设置它的标签属性并使用[button viewWithTag:yourLabelTag];

案例 2:您想删除 'titleLabel' 的UIButton(虽然我想不出它的原因)。做就是了[button.titleLabel removeFromSuperview];

案例 3:您没有 tag ,没有指向标签的指针,并且您想删除UILabel添加到按钮的所有实例:

for(int i = 0 ; i < button.subviews.count ; i++) //you could use for(UIView *v in button.subviews) but you shouldn't change the array during this kind of enumeration.
{
   UIView *v = [button.subviews objectAtIndex:i];
   if([v isMemberOfClass:[UILabel class]])
   {
      [v removeFromSuperview];
      i--;
   }
}

希望这可以帮助。

干杯!

于 2013-07-03T08:12:39.387 回答
0

最好的方法是tag为视图设置值并根据值删除视图tag

于 2013-07-03T07:46:13.560 回答