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より高速にする必要がある場合は、それらのaまたはプレーンC配列を保持します
于 2012-08-01T23:31:07.787 に答える