Objective-C 中相当于 .Net 委托的是什么?
在 .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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Cocoa 和 CocoaTouch 经常使用“目标/操作”。
从这个意义上来说,动作是等价的。
当执行目标/操作时,它将采用这种一般形式:
当然,参数列表在现实世界中会有所不同。
UIControl
类非常小 - 阅读它以获取有关该主题的更多信息。它将使您很好地了解控件的目标/操作支持以及用于处理这些事件的接口。 NSControl 是 Cocoa 的对应类,但它是一个更大的类 - 它定义的不仅仅是 Target/Action 接口。Cocoa and CocoaTouch often uses "Target/Action".
In that sense, the action is the equivalent.
When the target/action is performed, it would take this general form:
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.请注意,
onButtonDown:withEvent:
的第一个参数将是按钮对象,而不是触摸对象。您可以在 Cocoa 基础知识指南。
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.
实际上,它可能是这样的:
并且
此链接可能会有所帮助: http://bynomial.com/blog /?p=13
Actually, it might be something like this:
and
This link might help: http://bynomial.com/blog/?p=13