我有一个需求,导航栏默认隐藏。如果用户点击屏幕,它就会出现,如果用户第二次点击它就会消失,或者如果用户没有第二次点击它就会在 3 秒后消失。
所以我实现了这样的处理方法:
- (void) navigationBarDisplay
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(navigationBarHide) object:nil];
[[self navigationController] setNavigationBarHidden:NO animated:YES];
[self performSelector:@selector(hideNavigationBar) withObject:self afterDelay:3.0];
}
- (void) navigationBarHide
{
[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(navigationBarHide) object:nil];
[[self navigationController] setNavigationBarHidden:YES animated:YES];
}
我在每个函数的开头调用 cancelPreviousPerformRequestWithTarget 以取消之前对 performSelector:@selector(hideNavigationBar) 的任何未完成调用,从而重置内容,以便在显示导航栏后始终应用 3 的延迟。
但是如果我点击屏幕使导航栏出现,然后在 3 秒之前我再次点击它使其消失,然后第 3 次点击它使其重新出现,然后它被自动隐藏 3第一次点击后几秒,而不是第三次点击后 3 秒。
我也试过
[[NSRunLoop mainRunLoop ]cancelPerformSelector:@selector(navigationBarHide) target:self argument:nil];
但都是一样的
知道为什么这不起作用吗?或者是否有更好的解决方案?
最佳答案
在我看来,您的“取消”与“执行”不匹配:
NSObject cancelPreviousPerformRequestsWithTarget:self
selector:@selector(hideNavigationBar) object:nil
对比
[self performSelector:@selector(hideNavigationBar) withObject:self afterDelay:3.0];
在一种情况下,“对象”是nil
;另一个是 self
。
关于ios - cancelPreviousPerformRequestsWithTarget : isn't working for me,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8717177/