使用按钮操作删除UiView removeFromSuperview

时间:2014-10-23 11:02:24

标签: uiview uibutton ios8 xcode6

我对按钮的操作有问题...我不明白为什么事情不起作用,想问你是否可以帮我理解错误。

在我的viewController中,我在所选模式下有一个按钮,在另一个UIView里面添加了一个UIView,但当没有选中按钮时,应该取消之前输入的视图,它仍然没有做任何事情......我试过这个方式,但我不明白我在哪里

  - (IBAction)shareActive:(id)sender {
        UIView *checkActive = [[UIView alloc]init];
        checkActive.frame =CGRectMake(2, 2, 11, 11);
        checkActive.layer.masksToBounds = YES;
        checkActive.layer.cornerRadius = 5.5f;
        checkActive.backgroundColor = [UIColor greenColor];


        if (!self.condividiButton.selected) {

                self.condividiButton.selected = YES;
            [self.checkCondividi addSubview:checkActive];

                NSLog(@"attivo");

        }else {
            self.condividiButton.selected = NO;
                    [checkActive removeFromSuperview];

            NSLog(@"disattivo");


        }
    }

1 个答案:

答案 0 :(得分:1)

这是众多可能的解决方案之一。

我确信其他人会就如何解决这个问题给你不同的想法。

- (IBAction)buttonShareTouchedUpInside:(UIButton *)sender {
    if (!self.condividiButton.selected) {
        UIView *checkActive = [[UIView alloc]init];
        checkActive.tag = 121212;
        checkActive.frame =CGRectMake(2, 2, 11, 11);
        checkActive.layer.masksToBounds = YES;
        checkActive.layer.cornerRadius = 5.5f;
        checkActive.backgroundColor = [UIColor greenColor];

        self.condividiButton.selected = YES;
        [self.checkCondividi addSubview:checkActive];
    } else {
        self.condividiButton.selected = NO;
        [self.checkCondividi.subviews enumerateObjectsUsingBlock:^(UIView * obj, NSUInteger idx, BOOL *stop) {
            if (obj.tag == 121212) {
                [obj removeFromSuperview];
            }
        }];
    }
}

注意:原始问题可以通过许多其他可能更优雅的方式解决,但我并不关心这些问题。