扫码关注官方订阅号
让程序等待一段时间,不使用[NSThread sleepForTimeInterval:1.8]; ,不终止线程,而是什么都不做,等待一段时间,在这段时间里能接收到用户的点击事件,那要怎么写这个等待方法?
认证0级讲师
[obj performSelector:@selector(foo) withObject:bar afterDelay:1.8f];
最好在obj的dealloc中加上取消perform,防止对野指针perform selector
[NSObject cancelPreviousPerformRequestsWithTarget:self];
在你要等的线程中,增加runloop,这样线程就不会结束,如下(当然需要添加一个标志位,一边适时结束线程): while(!finished) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; }
微信扫码关注PHP中文网服务号
QQ扫码加入技术交流群
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
PHP学习
技术支持
返回顶部
最好在obj的dealloc中加上取消perform,防止对野指针perform selector
在你要等的线程中,增加runloop,这样线程就不会结束,如下(当然需要添加一个标志位,一边适时结束线程): while(!finished) { [[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]]; }