iOS sending parameter to selector using NSTimers

拈花ヽ惹草 提交于 2019-12-11 04:33:37

问题


Is there a way to send a parameter to the selector via a NSTimer ?

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

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

I know that using :

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

doesn't work, so I am asking, is there a way to do this?


回答1:


You can use the userInfo parameter for that:

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

But you will have to modify removeTheNote as follows:

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


来源:https://stackoverflow.com/questions/10086185/ios-sending-parameter-to-selector-using-nstimers

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!