开发者

How to Schedule Method call in Objective C

I am try to do multi-thr开发者_开发百科eading in Objective C. What I want to do now is that, for some instance of objects, I want to have to way to call some function 5 seconds later. How can I do that?

In Coco 2D, it's very easy to do it. They have something called scheduler. In Objective C, how to do it please?

Thanks


You can use performSelector:withObject:afterDelay:

For example:

[self performSelector:@selector(myFunc:) withObject:nil afterDelay:5.0];


Adding to what has been said, if you'd like to pass a single argument to myFunc, the call could be modified as follows

[self performSelector:@selector(showNote:) withObject:@"S" afterDelay:1.0];

and if you need to invoke a method that takes more than 1 argument, you can do that using invocation as shown in the following snippet -

SEL selector = @selector(nextPicture:);
NSMethodSignature *signature = [[self class] instanceMethodSignatureForSelector:selector];
NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:signature];
[invocation setSelector:selector];

//Set the arguments
[invocation setTarget:self];
NSString* str = [imageNames objectAtIndex:1];
[invocation setArgument:&str atIndex:2];
[NSTimer scheduledTimerWithTimeInterval:5.0f invocation:invocation repeats:NO];
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜