zeropt7
zeropt7

Reputation: 73

remove uiview by uibutton programmatically

I'm stuck at how to remove the uiview by user clicking the uibutton, the uibutton load at once the scene is loaded. The uibutton isn't getting response as well. How should I set it up? Please help, thanks a lot.

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

Upvotes: 2

Views: 2917

Answers (2)

Husrat Mehmood
Husrat Mehmood

Reputation: 2310

Use the following technique.Specify tag of every control that you have at your View.Then you can get benefit from this "tag" property

suppose i specified tag as 100. Then

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

now to remove it(Buttons) use this

[buttonToRemove removeFromSuperview];

Upvotes: 0

Hejazi
Hejazi

Reputation: 17015

Change the target from holderView to self:

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

and change the code that removes the view to the following:

[holderView removeFromSuperview];

Upvotes: 1

Related Questions