I need to pass the touches and event from touchesBegan to my own method called by a performSelector. I am using an NSInvocation to package up the arguments but I'm having problems with the target.
The reason that I do it this way is so I can handle other scroll events.
Here is my code:
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
UITouch *theTouch = [touches anyObject];
switch ([theTouch tapCount])
{
case 1:
NSInvocation *inv = [NSInvocation invocationWithMethodSignature: [self methodSignatureForSelector:@selector(handleTap: withEvent:)]];
[inv setArgument:&touches atIndex:2];
[inv setArgument:&event atIndex:3];
[inv performSelector:@selector(invokeWithTarget:) withObject:[self target] afterDelay:.5];
break;
}
}
Where handleTap is defined as:
-(IBAction)handleTap:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
My problem is that when I compile it I get a warning:
'CategoryButton' many not respond to '-target'
and when I run it, it crashes with a:
-[CategoryButton target]: unrecognized selector sent to instance 0x5b39280
I must admit I don't really understand what the target is trying to do here and how it is set.
Thanks for your help.
I think you need to take some time to read through your code carefully, line-by-line.
This isn't doing what you think it is. One half of one second after this method is executed, this will happen:
First, your class
CategoryButton
doesn't have a method calledtarget
. Second, why the delay? If you're using these touches for scrolling, a delay of 0.5 seconds is going to be extremely painful for users.Why are you using the NSInvocation class at all? If you really need the delay, you can simply use the
performSelector:
method on yourCategoryButton
instance:Notice the
performSelector:
methods only support one argument, so you have to wrap them in an NSArray. (Alternatively, you can use an NSDictionary.)You will have to update your
handleTap:
method to accept an NSArray/NSDictionary and snag the arguments as needed.But again, if you don't need the delay, why not just call the method yourself:
Maybe I'm misunderstanding your intentions, but it seems you're making this way more complicated than it needs to be.