How to pass paramerters to custom selector from a NSTimer object?
How to use the userInfo
object in an NSTimer
call such as
+ (NST开发者_如何学Cimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)ti target:(id)aTarget selector:(SEL)aSelector userInfo:(id)userInfo repeats:(BOOL)yesOrNo;
I want to send certain parameters to my custom selector.
The userInfo
parameter is for just that.
- (void)onTimer:(NSTimer *)timer
{
NSLog(@"User Info %@", [timer userInfo] );
}
The difficulty is that you need to wrap your parameters, even if more than 1, even if they aren't objects, into a single object. Creating a temporary NSDictionary, and stuffing it with keyed parameters works. Or you could create an custom class just to hold the required parameters, and create and fill an object of that class (alloc, initWithMy42Parameters:) to pass as the userInfo.
精彩评论