如何使用标记实际删除按钮

时间:2013-12-11 10:34:34

标签: ios objective-c tags addsubview nsindexpath

我将按钮subView添加到我单击的每一行。每次点击不同的行并将按钮子视图添加到我单击的另一行时,我试图删除按钮subView。

我的问题在哪里?它将subView添加到每一行而不删除任何一行。

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;

}

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

5 个答案:

答案 0 :(得分:2)

每次按钮都不要创建

每次在这里创建UIButton的新实例

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];
        }
   //It will just remove new instance of UIButton and no more your UIButton will be there on UIView.

试试这个

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

//Don't remove your button just change frame

如果您仍想删除按钮,请先删除

   [myButton removeFromSuperview];
   [myButton2 removeFromSuperview];

 //then create new instance of button

答案 1 :(得分:1)

尝试在按钮之前添加标签将其添加到子视图。

所以这个:

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

myButton.tag = myButton2.tag = 666;

应该是这样的:

myButton.tag = myButton2.tag = 666;

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

此致

hris.to

答案 2 :(得分:0)

如果您使用tableview单元格,请使用以下情况

1)在创建tableViewCell时将标签设置为按钮。

2)按cell.yourButton.hidden = YES

隐藏该按钮

3)当您点击行时,只需在self.selectedIndexPath = indexPath

中记下该indexPath为didSelectRowAtIndexPath:

4)现在重新加载你的tableView。只需在cellForRowAtIndexpath:中添加一个条件,如下所示。

if (indexPath.row == self.selectedIndexPath.row)
{
   cell.yourButton.hidden = NO;
}
else 
   cell.yourButton.hidden = YES;

答案 3 :(得分:0)

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

答案 4 :(得分:0)

我认为你必须先设置标记你的按钮,因为每个UIControl的默认标记都是0;