tags:

views:

105

answers:

2

I have some code where I display a sprite for N number of seconds. This box is removed after N seconds and I'd like to call a function that I can select. For example, in 10 seconds I want to call showBoxEnded.

I looked on here and saw I can use the SEL function. I wrote:

-(void)caller:(id)sender
{
    NSLog(@"Function Called!");
}

I can call callFunc in order to set the function that will be called:

-(void) callFunc:(SEL)func;

However, when I try this, my function is never called. Am I missing something here? Is this possible like it is in C++?

Is it possible to just pass the SEL function as a parameter to a function?

Thanks!

+3  A: 

You're looking for [self performSelector:@selector(myfunc) withObject: afterDelay:].

Graham Perks
I think you meant [self performSelector: withObject: afterDelay:];However, I'm curious as to what withObject would be. I tried sending nil, and self but get an exception. Any ideas?
David Nelson
The [docs](http://developer.apple.com/iphone/library/documentation/cocoa/reference/foundation/Classes/NSObject_Class/Reference/Reference.html#//apple_ref/occ/instm/NSObject/performSelector:withObject:afterDelay:) say you can pass nil for the object if the selector doesn't take an object. So that's not why you got an exception, edit your question adding those details if you still need help.
progrmr
Most likely you have the arg to @selector wrong, it needs to match your method signature. If your method takes one arg, you'd add a colon, such as @selector(myfunc:)
Graham Perks
You are obviously correct as the error message is:-[DialogBox dboxDone]: unrecognized selector sent to instance *** Terminating app due to uncaught exception 'NSInvalidArgumentException'I'm creating a CCSprite object (using cocos2d). then I create my DialogBox dbox object. Then:[dbox performSelector:@selector(dboxDone) withObject:nil afterDelay:2];my function is:-(void) dboxDone{ NSLog(@"Called!");}See anything wrong?
David Nelson
A: 

You can use timer and call a function after mentioned delay as:

timer = [NSTimer scheduledTimerWithTimeInterval:0.001 target:self selector:@selector(taskOnTimer) userInfo:nil repeats:NO];
neha