如何让performSelector 与NSInitation 一起工作?
我需要将触摸和事件从touchesBegan 传递到我自己的performSelector 调用的方法。我正在使用 NSInspiration 来打包参数,但我在目标方面遇到了问题。
我这样做的原因是这样我可以处理其他滚动事件。
这是我的代码:
- (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;
}
}
其中handleTap定义为:
-(IBAction)handleTap:(NSSet *)touches withEvent:(UIEvent *)event
{
[super touchesBegan:touches withEvent:event];
}
我的问题是,当我编译它时,我收到警告:
“CategoryButton”许多不响应“-target”
,当我运行它时,它崩溃并显示:
-[CategoryButton target ]: 无法识别的选择器发送到实例 0x5b39280
我必须承认我并不真正理解目标在这里试图做什么以及它是如何设置的。
感谢您的帮助。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为你需要花一些时间仔细地逐行阅读你的代码。
这并没有按照你的想法去做。执行此方法后半秒,将发生以下情况:
首先,您的类
CategoryButton
没有名为target
的方法。第二,为什么会拖延?如果您使用这些触摸来滚动,那么 0.5 秒的延迟对于用户来说将是非常痛苦的。你为什么要使用 NSInitation 类?如果您确实需要延迟,可以简单地在
CategoryButton
实例上使用performSelector:
方法:请注意,
performSelector:
方法仅支持一个参数,所以你必须将它们包装在 NSArray 中。 (或者,您可以使用 NSDictionary。)您必须更新您的
handleTap:
方法以接受 NSArray/NSDictionary 并根据需要获取参数。但同样,如果您不需要延迟,为什么不自己调用该方法:
也许我误解了您的意图,但似乎您使这种方式变得比需要的更加复杂。
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.
目标是您想要执行调用的对象。您遇到崩溃是因为您选择的对象 -
[self]
- 没有响应target
消息。我认为您可能只是对需要传入的内容感到有点困惑。使用当前代码,您要求在
self
target 属性上执行调用>。您可能不想这样做 - 我假设您希望仅在self
上执行调用。在这种情况下,请使用:The target is the object which you want the invocation to be performed on. You're getting a crash because your chosen object -
[self]
- doesn't respond to thetarget
message. I think you might just have got a bit confused over what you need to pass in.With your current code you're asking your invocation to be performed on the
target
property ofself
. You probably don't want to do this - I would assume you want your invocation to be performed simply onself
. In which case, use this: