定时器传参

2016-05-18  本文已影响127人  贵哥jk

开启一个需要传递参数的定时器

先上代码

- (void)creatTimer{
  //  id userInfo = @"贵哥jk";
  //  id userInfo = @{@"姓名": @"贵哥jk", @"email":@"942513675@qq.com"};
  //  id userInfo = @10;
    
    id userInfo = [UIButton new];
    ((UIButton *)userInfo).tag = 1000;
    
    NSTimeInterval time = 0.5;
    NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:time target:self selector:@selector(timerSelector:) userInfo:userInfo repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
}
- (void)timerSelector:(NSTimer *)timer{
    id userInfo = timer.userInfo;
    NSLog(@"%@",userInfo);

    UIButton *btn = userInfo;
    NSLog(@"%ld",btn.tag);

    if (@"条件判断") {
        [timer invalidate];
        timer = nil;
    }
}

解析

上一篇下一篇

猜你喜欢

热点阅读