首页 > 移动平台 > 详细

ios-NSRunLoop以及定时器NSTimer-理解

时间:2014-04-14 06:22:23      阅读:591      评论:0      收藏:0      [点我收藏+]
bubuko.com,布布扣
  [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeaction:) userInfo:@"liyang" repeats:NO];
       NSTimer *tt= [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeaction:) userInfo:@"limengxia" repeats:NO];
        [[NSRunLoop currentRunLoop]addTimer:tt forMode:NSDefaultRunLoopMode];
        [[NSRunLoop currentRunLoop]run];
//NSRunLoop的理解,每一个线程都有一个NSRunLoop对象,然而定时器也是在这个对象上面运行的,当一个线程运行完成了过后,会自动关闭线程,自然NSRunLoop也会被销毁,自然定时器就不会运行,为了不让其线程关闭,用此语句     [[NSRunLoop currentRunLoop]run];那么线程就会保持活跃状态,不会被关闭,自然定时器也就能用了,[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timeaction:) userInfo:@"liyang" repeats:NO];这么设置的定时器是自动添加到了NSRunLoop中的, NSTimer *tt= [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timeaction:) userInfo:@"limengxia" repeats:NO];这个定时器需要手动添加 [[NSRunLoop currentRunLoop]addTimer:tt forMode:NSDefaultRunLoopMode];
bubuko.com,布布扣

 

ios-NSRunLoop以及定时器NSTimer-理解,布布扣,bubuko.com

ios-NSRunLoop以及定时器NSTimer-理解

原文:http://www.cnblogs.com/liyang31tg/p/3662557.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!