如何使UIButton删除自己

时间:2013-02-11 03:27:38

标签: ios objective-c cocoa-touch

所以这是我的代码:

for (int x = 1; x < 100; x++) {
        int randomX = arc4random() %280;
        int randomY = arc4random() %500;

        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [myButton setTitle:string forState:UIControlStateNormal];
        myButton.frame = CGRectMake(randomX, randomY, 40.0, 40.0);
        [myButton addTarget:self action:@selector(buttonUp) forControlEvents:UIControlEventTouchUpInside];

        [self.view addSubview:myButton];

如果放手,如何让按钮自行删除?

谢谢, 波格丹

1 个答案:

答案 0 :(得分:3)

将选择器更改为

@selector(buttonUp:)

然后在你的方法定义中

- (void)buttonUp:(UIButton*)sender

添加

[sender removeFromSuperview]