美文网首页
1.4NSTimer

1.4NSTimer

作者: 草根小强 | 来源:发表于2019-04-08 16:07 被阅读0次

NSTimer

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    [self aboutTimer];
}
//在这个方法里,我们看下有关计时器的使用
- (void)aboutTimer{
    
    //1.实例化一个NSTimer
    //TimerInterval 执行事件的时间间隔 单位是秒
#warning 重点 target - action 机制\
    给 target 发送selector 这个消息\
    用 target 这个对象,来调用SEL 这个方法
    //target:接收消息的对象
    //selector: 消息的内容。
    
    //userinfo: 表示的是需要传递的参数。
    //repeats:是否重复 YES表示重复  NO表示只执行一次
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(showLog) userInfo:nil repeats:YES];
    //每个两秒钟,让self去调用showLog这个方法,重复执行
    //计时器默认再第一个时间间隔之后,首次执行
    
    //让计时器马上执行
    [timer fire];
    NSLog(@"程序启动了");
    
}
- (void)showLog{
    NSLog(@"计时器执行了");
}
@end
NSTimer.png

相关文章

网友评论

      本文标题:1.4NSTimer

      本文链接:https://www.haomeiwen.com/subject/dfhpiqtx.html