代码之家  ›  专栏  ›  技术社区  ›  arachide

如何删除动态ui按钮

  •  0
  • arachide  · 技术社区  · 15 年前

    我有一些代码可以创建动态按钮,如下所示:

    - (void)viewDidLoad {
    
        for (int i = 0; i < 9; i++)   
            for (int j = 0; j < 8; j++) {  
                forControlEvents:UIControlEventTouchDown]; 
                UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];
                button.frame = CGRectMake(10+i*34 , 130+j*30, 30 , 20 );
                [button setTitle:@"00" forState:  UIControlStateNormal];
                [button addTarget:self action:@selector(tapped:) forControlEvents:UIControlEventTouchUpInside];
                [self.view addSubview:button];
               button.tag = i;  
            } 
    }
    

    我希望删除动态uibuttons并重新创建它们。

    我该怎么办

    欢迎发表评论。

    谢谢 Interdev公司

    2 回复  |  直到 15 年前
        1
  •  4
  •   Giao    15 年前

    这将删除按钮

    [button removeFromSuperview]; 
    

    要删除按钮系列:

    for (int i = 0; i < 9; i++) {
        [[self.view viewWithTag:i] removeFromSuperview];
    }
    

    您有一个小问题,因为您的内部循环(使用j计数器的循环)正在创建8个按钮,但它们都有相同的标签。更改如何分配标记计数器,并调整上面的循环以使用该计数器,您应该能够删除所有按钮。

        2
  •  0
  •   willcodejavaforfood    15 年前

    这是一个疯狂的猜测:)

    [self.view removeSubview:button];