Question

I am adding button subView to each row I click. I am trying to remove the button subView every time once I click on different row and add button subview to the other row I click.

Where would be my issue? It is adding subView to each row without removing any of them.

if (sender.tag == 212)
  {
    myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    //Debug shows that every-time myButton&myButton2 hasn't tag??

    if (myButton2.tag != 0  && myButton.tag != 0)
    {
        [myButton removeFromSuperview];
        [myButton2 removeFromSuperview];
    }

    UITableViewCell *cell = (id)sender.superview.superview.superview;

    CGPoint center= sender.center;
    CGPoint rootViewPoint = [sender.superview convertPoint:center toView:commonTable.table];
    NSIndexPath *indexPath1 = [commonTable.table indexPathForRowAtPoint:rootViewPoint];

    itemdata1 = [itemList objectAtIndex:indexPath1.row];

    if (closeManager) [closeManager release];

    CGFloat __y = commonTable.y + commonTable.table.y - commonTable.table.contentOffset.y + cell.height*indexPath1.row + sender.y;
    CGFloat __x = commonTable.x + commonTable.table.x + sender.x;
    CGRect layerBox1 = CGRectMake(618, __y, 90, sender.height+3);
    CGRect layerBox2 = CGRectMake(888, __y, 94, sender.height+3);

    NSLog(@"%f and %f", __x, __y);

    myButton.frame = layerBox1;
    [[myButton layer] setBorderWidth:2.0f];
    myButton.layer.borderColor = [UIColor redColor].CGColor;
    [myButton addTarget:self action:@selector(func1:) forControlEvents:UIControlEventTouchUpInside];

    myButton2.frame = layerBox2;
    [[myButton2 layer] setBorderWidth:2.0f];
    [myButton2 addTarget:self action:@selector(func2:) forControlEvents:UIControlEventTouchUpInside];
    myButton2.layer.borderColor = [UIColor redColor].CGColor;

    [self addSubview:myButton];
    [self addSubview:myButton2];

    myButton.tag = myButton2.tag = 666;

}

Edited for Bhumeshwer katre:

    if (myButton&&myButton2)
    {
        [myButton removeFromSuperview];
        [myButton2 removeFromSuperview];
    }

    //Other variables 

    if(!myButton){
        myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        myButton.frame = layerBox1;
        [[myButton layer] setBorderWidth:2.0f];
        myButton.layer.borderColor = [UIColor redColor].CGColor;
        [myButton addTarget:self action:@selector(func1:) forControlEvents:UIControlEventTouchUpInside];
        myButton.tag = 666;
        [self addSubview:myButton];
    }
Was it helpful?

Solution

don't create each time your button like this

Here you are creating each time new instance of UIButton

myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];

And you are doing this

    //Debug shows that every-time myButton&myButton2 hasn't tag??
        if (myButton2.tag != 0  && myButton.tag != 0)
        {
            [myButton removeFromSuperview];
            [myButton2 removeFromSuperview];
        }
   //It will just remove new instance of UIButton and no more your UIButton will be there on UIView.

try like this

    if(!myButton){
        myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    }
    if(!myButton2) {
        myButton2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    }

//Don't remove your button just change frame

if you still want to remove buttons then first remove

   [myButton removeFromSuperview];
   [myButton2 removeFromSuperview];

 //then create new instance of button

OTHER TIPS

Try adding tags to buttons before adding them to subview.

So this:

    [self addSubview:myButton];
[self addSubview:myButton2];

myButton.tag = myButton2.tag = 666;

should become like this:

myButton.tag = myButton2.tag = 666;

[self addSubview:myButton];
[self addSubview:myButton2];

Regards,

hris.to

If you use tableview cell, Use this following cases,

1) Set tag to button while creating tableViewCell.

2) Hide that button by cell.yourButton.hidden = YES

3) When you click on row, just note down that indexPath as self.selectedIndexPath = indexPath in didSelectRowAtIndexPath:

4) Now reload your tableView. Just add one condition in cellForRowAtIndexpath: as below.

if (indexPath.row == self.selectedIndexPath.row)
{
   cell.yourButton.hidden = NO;
}
else 
   cell.yourButton.hidden = YES;
for (UIView *view in self.view.subView) { // instead of self.view you can use your main view
        if ([view isKindOfClass:[UIButton class]] && view.tag == ?) {
            UIButton *btn = (UIButton *)view;
            [btn removeFromSuperview];
        }
    }

i think you have to set tag your buttons first,because default tag of every UIControl is 0;

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