Dancer
Dancer

Reputation: 17651

Remove UINavigationBarButton SubView

I have the following method which adds a MKNumberBadgeView to the UINavigationBar of a UITableViewController -

-(void)counterBtn{

MKNumberBadgeView *numberBadge = [[MKNumberBadgeView alloc] initWithFrame:CGRectMake(25, -10, 40, 40)];
numberBadge.strokeColor = [UIColor colorWithRed:239.0/255.0 green:117.0/255.0 blue:33/255.0 alpha:0];
numberBadge.fillColor = [UIColor colorWithRed:239.0/255.0 green:117.0/255.0 blue:33/255.0  alpha:1];
numberBadge.shine = NO;
numberBadge.hideWhenZero = YES;
numberBadge.value = _countBtnNo;

NSLog(@"Value of count = %d", _countBtnNo);
[self.navigationController.navigationBar addSubview:numberBadge];

}

All works fine until I want to change the count value - I have the following function to change the value -

- (void)removeBtn{
    NSLog(@"ddd");
            _countBtnNo = 0;
    [self counterBtn];

}

But this does nothing - So i'm guessing I need to remove the subview first before re-adding - though there doesnt seem to be a removeSubview:numberBadge method - so i'm struggling!

Upvotes: 1

Views: 49

Answers (1)

Rafał Sroka
Rafał Sroka

Reputation: 40030

  1. Store a reference to your numberBadge in a property:

    @property(nonatomic, strong) MKNumberBadgeView *numberBadge;
    
  2. Then initialize it. To remove it, just call

    [_numberBadge removeFromSuperview];
    
  3. Alternatively you can just update the value of the counter. No need to remove it.

    _numberBadge.value = new value;
    

Upvotes: 1

Related Questions