Elijah Murray
Elijah Murray

Reputation: 2172

Problems Removing Subview from SuperView - Objective-C

Not sure why this isn't working to remove the added subview from the parent view. I want it to be removed if the longpress action is ended. I tested with an NSLog and I know that the gestures are properly registering as ended, but it's not removing the subview.

-(void)addSubview:(UILongPressGestureRecognizer *)gesture {
    NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self     options:nil];
    UIView *subView = [subviewArray objectAtIndex:0];

    if (gesture.state == UIGestureRecognizerStateBegan) {
        [self.view addSubview:subView];
        }
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [subView removeFromSuperview];
        }    
}

Desired functionality:

longpress starts: add subview.
longpress ends: remove subview.

Upvotes: 0

Views: 1029

Answers (1)

Utkarsh Goel
Utkarsh Goel

Reputation: 245

I hope you will resolve the issue by declaring UIView *subview in .h file and then putting below lines in viewDidLoad:

 NSArray *subviewArray = [[NSBundle mainBundle] loadNibNamed:@"overlayView" owner:self     options:nil];
    UIView *subView = [subviewArray objectAtIndex:0];

and then:

-(void)addSubview:(UILongPressGestureRecognizer *)gesture {
    if (gesture.state == UIGestureRecognizerStateBegan) {
        [self.view addSubview:subView];
        }
    if (gesture.state == UIGestureRecognizerStateEnded) {
        [subView removeFromSuperview];
        }    
}

Upvotes: 5

Related Questions