Question

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)];
}
Was it helpful?

Solution

Case 1: You added the label. In that case , set it's tag property and retrieve it with [button viewWithTag:yourLabelTag];

Case 2: You want to remove the 'titleLabel' of the UIButton ( although I cannot think of a reason for it ) . Just do [button.titleLabel removeFromSuperview];

Case 3: You have no tag , no pointer to the label and you want to remove all instances of UILabel added to the button:

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

Hope this helps.

Cheers!

OTHER TIPS

Best way is set tag value to the views and delete the views based on the tag values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top