NSTimer 简述
来源:互联网 发布:java项目遇到最大困难 编辑:程序博客网 时间:2024/05/21 04:20
日常开发中,我们经常会使用到NSTimer,看是简单但有复杂!
举例下面使用NSTimer 使用出现的问题
NSThread *Thread = [[NSThread alloc]initWithTarget:self selector:@selector(threed) object:nil]; [Thread start];-(void)threed{ NSThread *red= [NSThread currentThread]; if ([red isMainThread]) { NSLog(@"MainThread"); }else { NSLog(@"NotMainThread"); } NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(test ) userInfo:nil repeats:YES];}-(void)test{NSLog(@"run");{//会发现timer执行了, scheduledTimerWithTimeInterval 这个方法在子线程不跑了,如果想要他执行怎么办?需要引用另一个方法:timerWithTimeInterval 把上面的换成下面这样: NSTimer *timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(test ) userInfo:nil repeats:YES];[[NSRunLoop currentRunLoop] addTimer:timer forMode:NSDefaultRunLoopMode]; [[NSRunLoop currentRunLoop] run]; 这样就可以在子线运行了!
scheduledTimerWithTimeInterval,这个方法是自动回放入到默认的runloop中,除了主线程之外,其他线程的NSRunloop只有在调用[NSRunloop currentRunloop]才会创建。如有不对 请指点 !
1 0
- NSTimer 简述
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- NSTimer
- Java千百问_04异常处理(002)_java如何捕获异常
- Stanford 机器学习笔记 Week11 Application Example: Photo OCR
- VMware中CentOS设置静态IP
- 剑指offer-面试题10:二进制中1的个数
- 2010年山东省第一届ACM大学生程序设计竞赛:Balloons
- NSTimer 简述
- CodeForces 21 C Stripe 2
- Linux 下编译链接动静态库
- 阿里云上安装Git
- 你不知道的JavaScript--Item37 面向对象高级程序设计
- Swift UITextField/UITextView(placeholder的制作)
- Ubuntu下apache环境搭建
- 关于友盟推送收不到消息,有可能是用的不是友盟的V4包
- 嵌入式linux:LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别