Objective-C 中相当于 .Net 委托的是什么?

发布于 2024-12-28 04:07:11 字数 590 浏览 0 评论 0原文

在 .Net 中,当我有一个具有事件的对象时,我可以注册以通过委托处理该事件:

void Test()
{
    Button button = new Button();
    button.Click  += new EventHandler(OnClick);
}
void OnClick(object sender, EventArgs e)
{
    text1.Text = "The Button Was Clicked";
}

How do I do this kind of thing in Objective-C?具体来说,我正在尝试处理 SneakyButton 的 ccTouchEnded 。我以为会是这样的:

SneakyButton* mybutton = [SneakyButton button];
[mybutton ccTouchEnded:self.onButtonDown];

- (void)onButtonDown:(UITouch *)touch withEvent:(UIEvent *)event
{
    CCLOG(@"The Button Was Clicked");
}

In .Net when I have an object that has an event I can register to handle that event via a delegate:

void Test()
{
    Button button = new Button();
    button.Click  += new EventHandler(OnClick);
}
void OnClick(object sender, EventArgs e)
{
    text1.Text = "The Button Was Clicked";
}

How do I do this sort of thing in Objective-C? Specifically I'm trying to handle the ccTouchEnded of a SneakyButton. I thought it would be something like this:

SneakyButton* mybutton = [SneakyButton button];
[mybutton ccTouchEnded:self.onButtonDown];

- (void)onButtonDown:(UITouch *)touch withEvent:(UIEvent *)event
{
    CCLOG(@"The Button Was Clicked");
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

小…楫夜泊 2025-01-04 04:07:11

Cocoa 和 CocoaTouch 经常使用“目标/操作”。

  • 目标:一个 objc 对象。目标是收到消息的内容。
  • 操作:选择器。操作是向目标发送消息的选择器。

从这个意义上来说,动作是等价的。

当执行目标/操作时,它将采用这种一般形式:

[target performSelector:action withObject:someParameter];

当然,参数列表在现实世界中会有所不同。

UIControl 类非常小 - 阅读它以获取有关该主题的更多信息。它将使您很好地了解控件的目标/操作支持以及用于处理这些事件的接口。 NSControl 是 Cocoa 的对应类,但它是一个更大的类 - 它定义的不仅仅是 Target/Action 接口。

Cocoa and CocoaTouch often uses "Target/Action".

  • Target: an objc object. the target is what gets messaged.
  • Action: a selector. the action is the selector to message the target with.

In that sense, the action is the equivalent.

When the target/action is performed, it would take this general form:

[target performSelector:action withObject:someParameter];

of course, the parameter list will vary in the real world.

The UIControl class is quite small - read it for more info on the subject. It will give you a good idea of a control's target/action support and the interfaces you'll use for handling these events. NSControl is the Cocoa counterpart, but that's a much larger class - it defines much more than Target/Action interfaces.

你好,陌生人 2025-01-04 04:07:11
[mybutton addTarget:self action:@selector(onButtonDown:withEvent:)
    forControlEvents:UIControlEventTouchUpInside];

请注意,onButtonDown:withEvent: 的第一个参数将是按钮对象,而不是触摸对象。

您可以在 Cocoa 基础知识指南

[mybutton addTarget:self action:@selector(onButtonDown:withEvent:)
    forControlEvents:UIControlEventTouchUpInside];

Note that the first argument to onButtonDown:withEvent: will be the button object, not the touch object.

You can read more about the target/action pattern in the Cocoa Fundamentals Guide.

把时间冻结 2025-01-04 04:07:11

实际上,它可能是这样的:

[mybutton ccTouchEnded:self.onButtonDown];

并且

(void)myButtonClick:(id)sender {
    mylabel.text = @"The Button Was Clicked";
}

此链接可能会有所帮助: http://bynomial.com/blog /?p=13

Actually, it might be something like this:

[mybutton ccTouchEnded:self.onButtonDown];

and

(void)myButtonClick:(id)sender {
    mylabel.text = @"The Button Was Clicked";
}

This link might help: http://bynomial.com/blog/?p=13

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文