0

对于这种情况,删除以编程方式创建的按钮的代码是什么,例如:

for (m=0; m<f;m++ )
    {
        numerodeboton=partenumero+m+1;
        //NSLog(@"crear boton2, %i", numerodeboton);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [button setBackgroundImage:[UIImage imageNamed:@"boton.png"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(notasCurso)forControlEvents:UIControlEventTouchUpInside];
        [button setTitle:[NSString stringWithFormat:@"Botón %d", numerodeboton] forState:UIControlStateNormal];
        button.frame = CGRectMake(espacioh+m*(h+d)-z + h/2, y + (l-1)*(v+d) + v/2, 1, 1);
        button.layer.cornerRadius = 30;
        button.clipsToBounds = YES;
        button.layer.borderColor=[UIColor blackColor].CGColor;
        button.layer.borderWidth=0.01f;
        [button setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        button.tag = numerodeboton;
        [UIView animateWithDuration:0.05*numerodeboton animations:^{
            button.frame = CGRectMake(espacioh+m*(h+d)-z, y + (l-1)*(v+d), h, v);
        }];
        [self.view addSubview:button];
    }

假设我想删除带有 的按钮tag = 3,代码是什么?

4

2 回答 2

4

该行将[[self.view viewWithTag:3] removeFromSuperview];获取带有标签 3 的按钮,然后将其删除。如果您有多个标签为 3 的按钮,只需像这样遍历它们:

while (UIView *aView = [self.view viewWithTag:3]) {
    [aView removeFromSuperview];
}
于 2012-08-01T23:28:39.197 回答
0

我想更安全的方法是使用[button removeFromSuperview],这将在它被保留后自动释放内部视图addSubView:

当然,您需要一种方法来检索正确的按钮,您可以

  • 取回它viewWithTag:
  • NSMutableArray如果您需要更快的速度,请保留它们的一个或一个普通的 C 数组
于 2012-08-01T23:31:07.787 回答