通过uibutton以编程方式删除uiview

时间:2012-01-03 18:26:05

标签: ios uiview uibutton addsubview

我坚持如何通过用户点击uibutton删除uiview,在加载场景时立即加载uibutton。 uibutton也没有得到回应。 我该如何设置?请帮助,非常感谢。

- (void)viewWillAppear:(BOOL)animated {
    UIView *holderView = [[UIView alloc] initWithFrame:CGRectMake(0, 50, _imagePicker.selectedImage.size.width, _imagePicker.selectedImage.size.height)];
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:[holderView frame]];
    [imageView setImage:_imagePicker.selectedImage];
    [holderView addSubview:imageView];

    UIButton *removeSticker = [UIButton buttonWithType:UIButtonTypeCustom];
    removeSticker.frame = CGRectMake(0, 0, 200, 100);
    [removeSticker setImage:[UIImage imageNamed:@"cancel-disabled.png"] forState:UIControlStateNormal];
    [removeSticker addTarget:holderView action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    [holderView addSubview: removeSticker];

    UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(scale:)];
    [pinchRecognizer setDelegate:self];
    [holderView addGestureRecognizer:pinchRecognizer];

    UIRotationGestureRecognizer *rotationRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotate:)];
    [rotationRecognizer setDelegate:self];
    [holderView addGestureRecognizer:rotationRecognizer];

    UIPanGestureRecognizer *panRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(move:)];
    [panRecognizer setMinimumNumberOfTouches:1];
    [panRecognizer setMaximumNumberOfTouches:1];
    [panRecognizer setDelegate:self];
    [holderView addGestureRecognizer:panRecognizer];

    UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapped:)];
    [tapRecognizer setNumberOfTapsRequired:1];
    [tapRecognizer setDelegate:self];
    [holderView addGestureRecognizer:tapRecognizer];

    [parentPreviewView addSubview:holderView];
}

- (void) buttonClicked: (id)sender
{
    [self.view removeFromSuperview];
}

2 个答案:

答案 0 :(得分:1)

将目标从holderView更改为self

[removeSticker addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];

并将删除视图的代码更改为以下内容:

[holderView removeFromSuperview];

答案 1 :(得分:0)

使用以下技术。指定View中每个控件的标记。然后您可以从此“标记”属性中获益

假设我将标签指定为100.然后

 UIButtom *buttonToRemove=(UIButton*)[self.view   viewWithTag:100];

现在删除它(按钮)使用此

[buttonToRemove removeFromSuperview];