Never_be
Never_be

Reputation: 849

removeFromSuperview not working

To my view I add imageView but after I remove it this image stay on view, how remove it ?

Create:

(void)handleTapHold:(UILongPressGestureRecognizer *)gestureRecognizer{
UIImageView *pinkIm = [[UIImageView alloc]initWithFrame:CGRectMake(self.roundView.frame.origin.x + 70, self.roundView.frame.origin.y - 70, 60, 60)];
[pinkIm setImage:[UIImage imageNamed:@"pink_print"]];
pinkIm.layer.cornerRadius = pinkIm.frame.size.height / 2;
pinkIm.layer.borderWidth = 3.0f;
pinkIm.layer.borderColor = [UIColor whiteColor].CGColor;
pinkIm.clipsToBounds = YES;

[self.view addSubview:pinkIm];

 if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
  [pinkIm removeFromSuperview];
 }
}

later in same method I try to Remove but nothing happen:

 [pinkIm removeFromSuperview];

Find my problem, in start I don't add:

if (gestureRecognizer.state == UIGestureRecognizerStateBegan){

Upvotes: 0

Views: 2408

Answers (2)

Max_Power89
Max_Power89

Reputation: 1770

Be sure that you're triggering the method on the main thread, try this:

 if (gestureRecognizer.state == UIGestureRecognizerStateEnded){
        dispatch_async(dispatch_get_main_queue(), { () -> Void in
         [pinkIm removeFromSuperview];
    })
 }

Upvotes: 0

Dean Leitersdorf
Dean Leitersdorf

Reputation: 1341

It might be that by the time you try to remove it, *pinkIm is already nil - could you post the rest of the code so that we can make sure the pointer pinkIm is not nil?

In the case that it is nil, and there is nothing you can do about it, the other way to remove it would be to find it through the current view's children. Example:

for(UIView *child in self.children){
if([child isKindOfClass: [UIImageView class]]){
[child removeFromSuperView];
break;}}

Note: The above will not work if you have several image views. In that case, you may want to keep checking the state of "child" to ensure that you are talking about what *pinkIm was pointing to.

Please post rest of code of the method!

Upvotes: 1

Related Questions