有没有一种方法可以通过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];
不起作用,所以我问,有办法吗?
最佳答案
您可以为此使用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);
}