IOS中延迟执行的几种方法
2015-04-07 17:10
204 查看
前几天去国美在线面试,就遇到了上面的问题,当时是笔试,只写出来了第一种方法,现在整理了一下。
runDelayMethod方法就是要延迟执行的方法
//1.performSelector方法;在当前线程中执行的方法,使用默认模式,并延迟执行@selector方法。 [self performSelector:@selector(runDelayMethod) withObject:nil afterDelay:2.0]; //取消之前注册的performSelector:withObject:afterDelay:这个方法 //[NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(runAfterDelay) object:nil]; //2.GCD方法;延迟执行block(块)中代码 dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ [self runDelayMethod]; }); //3.NSTimer方法;创建一个NSTimer对象,使用默认的模式,延迟执行@selector方法 [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(runDelayMethod) userInfo:nil repeats:NO];
runDelayMethod方法就是要延迟执行的方法
- (void)runDelayMethod { NSLog(@"延迟两秒执行"); }
相关文章推荐
- iOS开发中方法延迟执行的几种方式
- 浅谈iOS开发中方法延迟执行的几种方式
- 浅谈iOS开发中方法延迟执行的几种方式
- iOS开发中方法延迟执行的几种方式
- iOS 延迟执行的几种方法
- iOS开发 方法延迟执行的几种方式
- ios延迟执行的几种方法
- iOS开发中方法延迟执行的几种方式
- iOS 开发中方法延迟执行的几种方式
- iOS开发中方法延迟执行的几种方式
- 浅谈iOS开发中方法延迟执行的几种方式
- iOS开发中方法延迟执行的几种方式-简书
- 浅谈iOS开发中方法延迟执行的几种方式(转载)
- 浅谈iOS开发中方法延迟执行的几种方式
- 关于iOS中延迟执行的几种方法
- 浅谈iOS开发中方法延迟执行的几种方式
- iOS --延迟执行的几种方法
- 浅谈iOS开发中方法延迟执行的几种方式
- 关于iOS中延迟执行的几种方法
- iOS延迟执行的三种方法