Sajad Khan
Sajad Khan

Reputation: 522

UITouch not releasing view

I have a custom view which is not being deallocated. I dismiss controller on close button pressed. Now if I only press the button the view is deallocated alright. But If press button with one finger with other finger touching the view its not deallocated on dismiss but on the next touch event.

Its UITouch which is keeping the reference of my view and not releasing it. How can I fix this?

Here is my code for my close action:

- (IBAction)closePressed:(UIButton *)sender {
    NSLog(@"Close pressed"); 
    if (self.loader)
    [self.loader cancelJsonLoading];
    [self.plView quit];
    [self dismissViewControllerAnimated:YES completion:nil];
}

Upvotes: 4

Views: 97

Answers (1)

Nicolas Buquet
Nicolas Buquet

Reputation: 3955

Did you try to call:

[self.view resignFirstResponder];

That should cancel all pending UITouches.

If this doesn't work, you can keep trace of your touches:

  • define a NSMutableSet where you store current touches:

    NSMutableSet *_currentTouches;

  • in your init():

    _currentTouches = [[NSMutableSet alloc] init];

And implement:

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super.touchesBegan:touches withEvent:event];
    [_currentTouches unionSet:touches]; // record new touches
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super.touchesEnded:touches withEvent:event];
    [_currentTouches minusSet:touches]; // remove ended touches
}

- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super.touchesEnded:touches withEvent:event];
    [_currentTouches minusSet:touches]; // remove cancelled touches
}

Then, when you need to clean your touches (when you release your view for instance):

- (void)cleanCurrentTouches {
    self touchesCancelled:_currentTouches withEvent:nil];
    _currentTouchesremoveAllObjects];
}

It is, I think, a bit hacky, but the doc says:

When an object receives a touchesCancelled:withEvent: message it should clean up any state information that was established in its touchesBegan:withEvent: implementation.

Upvotes: 1

Related Questions