有没有办法通过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];
不起作用,所以我问,有没有办法做到这一点?
答案 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);
}