문제

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)];
}
도움이 되었습니까?

해결책

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!

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top