1

I have applied a custombadge as a subview of a UIButton with the help of @Midhun MP in this question - Adding a subview to UIButton.

All works great - but I'm now trying to remove the subview when the button is clicked.

I've setup a method thats triggered via a click on one of the buttons

- (void)hide
{
    //Hide Buttons
    _importantMsg.hidden = YES;


    [CustomBadge removeFromSuperview];

}

But I'm struggling to hide the subview - I've tried -

[CustomBadge removeFromSuperview];

and

_MsgHeadBtn.subviews.hidden = YES;;

but i get unknown receiver errors - can anyone point me in thh right direction pls?

errors for each try show in screenshot below -

enter image description here

Community
  • 1
  • 1
Dancer
  • 17,035
  • 38
  • 129
  • 206

4 Answers4

2

You need either a reference to the custom view so you can remove it, iVar or alternatively give the view a tag then use button viewForTag method and remove that way.

i.e.

@implementation ViewController {
   UIView *_customView;
}

-(void)addCustomsubView
{
   _customView = [UIView alloc] init …./ your initialize

   [button addSubView:_customView];
}

- (void)hide
{
   [_customView removeFromSuperView];
   _customView = nil;
}
darren102
  • 2,830
  • 1
  • 22
  • 24
  • try _customView.hidden = YES; see if that hides it, if it does then something with removing the customView from the super view is the issue – darren102 Oct 31 '13 at 13:51
  • that works perfectly - is it a problem to just use hidden = yes? – Dancer Oct 31 '13 at 15:33
  • Well the removeFromSuperView should work that is the main issue around this, hidden was just to verify that it was still attached to the iVar but in this case removeFromSuperView should work correctly. You could create with a tag and use viewForTag instead in order to verify since removeFromSuperView should be working correctly – darren102 Oct 31 '13 at 16:03
1

try with this

the method which can have the button reference if you created button dynamically.

 -(void)hide:(UIButton *)sender
 {
   for(UIView *viewRef in sender.subviews)
    {
     if(viewRef isKindofClass:[CustomBadge class])
     {
      [viewRef removeFromSuperview];
     }
    }

 }
wesley
  • 859
  • 5
  • 15
0

The first problem is _MsgHeadBtn.subviews.hidden = YES; .subviews is an array, that you cannot perform the setHidden selector on. The second issue-ish is that there's no point in hiding a view if you are removing it right after. Third problem is that

 [CustomBadge removeFromSuperview];

CustomBadge is not a instance of a class, an object, it is just a class. The theoratical approach would be smth like

-(void)addSomeView {
    //customBadge MUST be an instance variable or a property, otherwise you are not holding a reference to it so you can access it later
    customBadge = [[CustomBagde alloc] initWith...];
    [yourButton addSubView:customBadge];
}


-(void)hide {
    //This is where the app will crash if customBadge is not a property or an instance variable
    [customBadge removeFromSuperView];
}
Durican Radu
  • 1,327
  • 11
  • 12
0

You can use a tag on your custom subview to remove it later. Use this to add your custom view:

customBadge1.tag=1;
[_MsgHeadBtn addSubview:customBadge1];

and this to remove it

customBadge=[_MsgHeadBtn viewWithTag:1];
[customBadge removeFromSuperview]
slecorne
  • 1,720
  • 1
  • 11
  • 14