tags:

views:

111

answers:

2

Hello I have some codes to create dynamic button as below:

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

I hope to delete the dynamic uibuttons and recreate them.

How can I do

Welcome any comment.

Thanks interdev

A: 

A wild guess here :)

[self.view removeSubview:button];
willcodejavaforfood
error: 'button' undeclared
Yes you have to identify the button first, but see Giao's answer how to use the tag you created to do it properly and ignore me :)
willcodejavaforfood
+3  A: 

This removes the button

[button removeFromSuperview]; 

To remove the series of button:

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

You have a bit of a minor problem because your inner loop (the one using the j counter) is creating 8 buttons but they all have the same tag. Change how you assign the tag counter and adjust the loop above to use that counter and you should be able to remove all the buttons.

Giao