jjzjj

ios - cancelPreviousPerformRequestsWithTarget 未取消未完成的 performSelector :withDelay

coder 2023-07-29 原文

我正在使用 UIWebView 并且不希望导航栏出现,除非用户点击屏幕上任何非链接的地方。 所以我有这段代码在延迟后显示导航栏:

- (void)handleTapGesture:(UITapGestureRecognizer *)sender  
{      
.... 
[self performSelector:@selector(showNavigationBar) withObject:self afterDelay:0.2]; 
}

我不会在点击处理程序被调用时立即调用 showNavigationBar,因为用户可能已经点击了一个链接,在这种情况下点击处理程序被调用之前 UIWebView shouldStartLoadWithRequest,因此如果我将导航栏隐藏在 shouldStartLoadWithRequest 中,它会瞬间闪烁到屏幕上。 因此,我将其设置为在延迟后显示,这为以下代码在 shouldStartLoadWithRequest 内执行提供了时间(如果用户没有点击链接,则 shouldStartLoadWithRequest 不是' t 调用并显示导航栏,在这种情况下应该如此)。

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType  
{ 
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showNavigationBar) object:nil];
...

但是这不起作用,我已经将延迟时间增加到几秒并且可以确认 cancelPreviousPerformRequestWithTarget 在导航栏显示之前被调用,但是当指定的时间过去时显示。 cancelPreviousPerformRequestWithTarget 无效。

有人知道为什么它不起作用吗?

最佳答案

您的执行与您的取消不符。在执行中,您将自己作为对象传递:

[self performSelector:@selector(showNavigationBar) withObject:self afterDelay:0.2]; 

在取消中,您将 nil 作为对象传递:

[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(showNavigationBar) object:nil];

他们不匹配,所以延迟执行不应该被取消。

关于ios - cancelPreviousPerformRequestsWithTarget 未取消未完成的 performSelector :withDelay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8697648/

有关ios - cancelPreviousPerformRequestsWithTarget 未取消未完成的 performSelector :withDelay的更多相关文章

随机推荐