我有一个未被解除分配的自定义视图。我按下关闭按钮时关闭控制器。现在如果我只按下按钮,视图就会被解除分配。但是,如果用一根手指按下按钮,其他手指触摸视图,则在解除时不会取消分配,而是在下一次触摸事件中。
它的UITouch保持我的视图的参考而不释放它。我怎样才能解决这个问题?
以下是我的近距离行动代码:
- (IBAction)closePressed:(UIButton *)sender {
NSLog(@"Close pressed");
if (self.loader)
[self.loader cancelJsonLoading];
[self.plView quit];
[self dismissViewControllerAnimated:YES completion:nil];
}
答案 0 :(得分:1)
您是否尝试过致电:
[self.view resignFirstResponder];
这应取消所有未决的UITouches。
如果这不起作用,您可以跟踪您的触摸:
定义存储当前触摸的NSMutableSet:
NSMutableSet *_currentTouches;
:
_currentTouches = [[NSMutableSet alloc] init];
并实施:
- (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
}
然后,当您需要清理触摸时(例如,当您释放视图时):
- (void)cleanCurrentTouches {
self touchesCancelled:_currentTouches withEvent:nil];
_currentTouchesremoveAllObjects];
}
我认为,这有点苛刻,但是医生说:
当一个对象收到touchesCancelled:withEvent:消息时 应该清理在其中建立的任何国家信息 touchesBegan:withEvent:implementation。