1

有没有办法通过 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];

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

4

1 回答 1

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);
} 
于 2012-04-10T09:39:07.970 回答