ios - UITapGestureRecognizer 在关闭 viewController 后导致崩溃
<p><p>尝试将 <code>UITapGestureRecognizer</code> 实现到表单模态视图 Controller 。
如果用户在表单之外触摸,表单应该关闭,这样代码就可以正常工作了。</p>
<p>问题是如果我手动关闭表单并尝试触摸任何视点,它仍然会尝试调用 <code>UITapGestureRecognizer</code> 方法并且应用程序崩溃。</p>
<pre><code>Error ::
-: message sent to deallocated instance
-(void)done
{
;
//send notification that folder has been created
[ postNotificationName:@"refreshDetails" object:nil];
}
-(void)viewDidAppear:(BOOL)animated
{
// gesture recognizer to cancel screen
UITapGestureRecognizer *recognizer = [ initWithTarget:self action:@selector(handleTapBehind:)];
;
recognizer.cancelsTouchesInView = NO; //So the user can still interact with controls in the modal view
;
}
- (void)handleTapBehind:(UITapGestureRecognizer *)sender
{
if (sender.state == UIGestureRecognizerStateEnded)
{
CGPoint location = ; //Passing nil gives us coordinates in the window
//Then we convert the tap's location into the local view's coordinate system, and test to see if it's in or outside. If outside, dismiss the view.
if (! withEvent:nil])
{
// Remove the recognizer first so it's view.window is valid.
;
;
}
}
}
</code></pre>
<p>为什么在我关闭 ViewController 后仍然调用 <code>handleTapBehind:</code>?我怎样才能解决这个问题? </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>给窗口添加手势识别器:</p>
<pre><code>;
</code></pre>
<p>并将目标设置为您的 Controller ;</p>
<p>因此,当您的 Controller 关闭时 - 它已被释放,但手势识别器仍然存在。当它触发时,它会尝试向您的 Controller 发送操作,但该 Controller 已经不存在了。</p>
<p>因此,您应该将识别器添加到 Controller 的 View 中或在 viewWillDissaper 方法中将其删除。</p></p>
<p style="font-size: 20px;">关于ios - UITapGestureRecognizer 在关闭 viewController 后导致崩溃,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/14588061/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/14588061/
</a>
</p>
页:
[1]