I’m trying to use a NSTimer to call a function in an class’s superclass.
The function works if I call it like this:
[super playNarrationForPage:[NSNumber numberWithInt:1]];
But if I do this:
NSTimer *narrationTimer = [NSTimer scheduledTimerWithTimeInterval:7.5
target:self.superclass
selector:@selector(playNarrationForPage:)
userInfo:[NSNumber numberWithInt:1]
repeats:NO
];
I get this error: unrecognized selector sent to class 0x106890
2012-07-06 21:14:59.522 MyApp[19955:707] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '+[MyBaseClass playNarrationForPage:]: unrecognized selector sent to class 0x106890'
I’ve tried setting super as the target: but I’m told “Use of undeclared identifier super”. Any thoughts?
Since
self.superclassreturns aClassobject, it’s definitely not what you want.Also, you don’t seem to understand how an
NSTimercalls its target. You’re using anNSNumberas the timer’suserInfo, and yourplayNarrationForPage:method seems to expect anNSNumberargument. But anNSTimerdoes not pass itsuserInfoas the argument when it calls its target! TheNSTimerpasses itself as the argument, like this:You must create a new method for your timer to call. This new method needs to take the timer as its argument, like this:
Then you need to use that selector when you create your timer:
If you really want to make
narrationTimerDidFire:call[super playNarrationForPage:], the compiler will let you. But doing that is very fishy. If you haven’t overriddenplayNarrationForPage:in your subclass, then there’s no reason to refer tosuperdirectly; your subclass inherits its superclass’splayNarrationForPage:method. If you have overriddenplayNarrationForPage:in your subclass, then bypassing it from your timer’s callback indicates that something is wrong with your design.