如何让performSelector 与NSInitation 一起工作?

发布于 2024-10-17 17:31:18 字数 1118 浏览 4 评论 0原文

我需要将触摸和事件从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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

输什么也不输骨气 2024-10-24 17:31:18

我认为你需要花一些时间仔细地逐行阅读你的代码。

[inv performSelector:@selector(invokeWithTarget:) withObject:[self target] afterDelay:.5];

这并没有按照你的想法去做。执行此方法后半秒,将发生以下情况:

[inv invokeWithTarget:[self target]];

首先,您的类 CategoryButton 没有名为 target 的方法。第二,为什么会拖延?如果您使用这些触摸来滚动,那么 0.5 秒的延迟对于用户来说将是非常痛苦的。

你为什么要使用 NSInitation 类?如果您确实需要延迟,可以简单地在 CategoryButton 实例上使用 performSelector: 方法:

NSArray *params = [NSArray arrayWithObjects:touches, event, nil];
[self performSelector:@selector(handleTap:) withObject:params afterDelay:0.5];

请注意,performSelector: 方法仅支持一个参数,所以你必须将它们包装在 NSArray 中。 (或者,您可以使用 NSDictionary。)

您必须更新您的 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;
    }
}

也许我误解了您的意图,但似乎您使这种方式变得比需要的更加复杂。

I think you need to take some time to read through your code carefully, line-by-line.

[inv performSelector:@selector(invokeWithTarget:) withObject:[self target] afterDelay:.5];

This isn't doing what you think it is. One half of one second after this method is executed, this will happen:

[inv invokeWithTarget:[self target]];

First, your class CategoryButton doesn't have a method called target. 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 your CategoryButton instance:

NSArray *params = [NSArray arrayWithObjects:touches, event, nil];
[self performSelector:@selector(handleTap:) withObject:params afterDelay:0.5];

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:

- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{           
    UITouch *theTouch = [touches anyObject];

    switch ([theTouch tapCount]) 
    {
        case 1:
            [super touchesBegan:touches withEvent:event];
        break;
    }
}

Maybe I'm misunderstanding your intentions, but it seems you're making this way more complicated than it needs to be.

还给你自由 2024-10-24 17:31:18

我必须承认我不太明白目标在这里想要做什么以及它是如何设置的。

目标是您想要执行调用的对象。您遇到崩溃是因为您选择的对象 - [self] - 没有响应 target 消息。我认为您可能只是对需要传入的内容感到有点困惑。

使用当前代码,您要求在 selftarget 属性上执行调用>。您可能不想这样做 - 我假设您希望仅在 self 上执行调用。在这种情况下,请使用:

[inv performSelector:@selector(invokeWithTarget:) withObject:self afterDelay:.5];

I must admit I don't really understand what the target is trying to do here and how it is set.

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 the target 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 of self. You probably don't want to do this - I would assume you want your invocation to be performed simply on self. In which case, use this:

[inv performSelector:@selector(invokeWithTarget:) withObject:self afterDelay:.5];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文