iOS使用NSTimers将参数发送到选择器

时间:2012-04-10 09:29:09

标签: ios parameters selector nstimer

有没有办法通过NSTimer将参数发送到选择器?

myTimer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(removeTheNote:) userInfo:nil repeats:NO];

      - (void)removeTheNote:(NSString*)note
    {
        NSLog(@"Note %@ ----------- REMOVED!",note);
    }

我知道使用:

myTimer =[NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(removeTheNote:myNote) userInfo:nil repeats:NO];

不起作用,所以我问,有没有办法做到这一点?

1 个答案:

答案 0 :(得分:10)

您可以使用userInfo参数:

myTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(removeTheNote:) userInfo:myNote repeats:NO];

但您必须按如下方式修改removeTheNote

- (void)removeTheNote:(NSTimer *)timer
{
    NSString *note = timer.userInfo;
    NSLog(@"%@", note);
}