开发者

How to pass paramerters to custom selector from a NSTimer object?

开发者 https://www.devze.com 2023-01-15 15:52 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消