Iphone 如何让performSelector与NSInvocation一起工作?
我需要将touchsbegind中的触摸和事件传递到performSelector调用的我自己的方法。我正在使用NSInvocation打包参数,但目标有问题 我这样做的原因是为了处理其他滚动事件 这是我的密码:Iphone 如何让performSelector与NSInvocation一起工作?,iphone,nsinvocation,Iphone,Nsinvocation,我需要将touchsbegind中的触摸和事件传递到performSelector调用的我自己的方法。我正在使用NSInvocation打包参数,但目标有问题 我这样做的原因是为了处理其他滚动事件 这是我的密码: - (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event { UITouch *theTouch = [touches anyObject]; switch ([t
- (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”
当我运行它时,它会崩溃:
-[类别按钮目标]:发送到实例0x5b39280的选择器无法识别
我必须承认,我真的不明白目标在这里试图做什么,以及它是如何设定的
谢谢你的帮助
我必须承认,我真的不明白目标在这里试图做什么,以及它是如何设定的
目标是要对其执行调用的对象。由于您选择的对象-[self]
-没有响应目标
消息,因此您将发生崩溃。我想你可能只是对你需要传递什么有点困惑
使用当前代码,您要求对self
的target
属性执行调用。您可能不想这样做-我假设您希望调用只在self
上执行。在这种情况下,请使用以下命令:
[inv performSelector:@selector(invokeWithTarget:) withObject:self afterDelay:.5];
我认为您需要花一些时间逐行仔细阅读代码
[inv performSelector:@selector(invokeWithTarget:) withObject:[self target] afterDelay:.5];
这不是你想象的那样。执行此方法后的半秒,将发生以下情况:
[inv invokeWithTarget:[self target]];
首先,您的类CategoryButton
没有名为target
的方法。第二,为什么拖延?如果您使用这些触摸进行滚动,0.5秒的延迟对用户来说将是非常痛苦的
为什么要使用NSInvocation类?如果确实需要延迟,只需在CategoryButton
实例上使用performSelector:
方法即可:
NSArray *params = [NSArray arrayWithObjects:touches, event, nil];
[self performSelector:@selector(handleTap:) withObject:params afterDelay:0.5];
请注意,performSelector:
方法只支持一个参数,因此必须将它们包装在NSArray中。(或者,您可以使用NSD字典。)
您必须更新handleTap:
方法以接受NSArray/NSDictionary并根据需要捕获参数
但是,如果您不需要延迟,为什么不自己调用该方法:
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
UITouch *theTouch = [touches anyObject];
switch ([theTouch tapCount])
{
case 1:
[super touchesBegan:touches withEvent:event];
break;
}
}
也许我误解了你的意图,但你似乎让这条路变得比需要的更复杂。感谢你帮助传递参数,因为NSArray是拼图中缺少的部分。延迟用于管理单击。如果在此时间内出现滚动,则取消单击。这种方法似乎效果很好。啊,我明白了!很高兴事情进展顺利。忽略我的怀疑,我只是看到太多人试图与框架抗争我实际上使用了上面的方法,这意味着我可以完全摆脱NSINVIGATION。我想你是对的,赛尔夫会没事的。