addTarget:action:forControlEvents: 方法是如何编程的?

发布于 2024-11-08 22:59:19 字数 337 浏览 0 评论 0原文

在 UIKit 中,UIControl 有以下方法:

  • addTarget:action:forControlEvents:
  • removeTarget:action:forControlEvents:

我很想知道这些方法是如何编程的。有人可以指出这些方法如何编程的资源(或提供如何编程的示例)吗?

我已经为我自己的正在运行的项目实现了类似的东西。然而,如果能知道解决这个问题的最佳技术,或者苹果是如何实现它的,那就太好了,因为它是一个有用的模式。 (而且,如果我是正确的,它与委托模式不同?)。

In UIKit, UIControl has the following methods:

  • addTarget:action:forControlEvents:
  • removeTarget:action:forControlEvents:

I am interested to know how these are programmed. Could someone point to a resource of how these methods are programmed (or provide an example how these are programmed)?

I have implemented something similar for my own project that is working. However it would be great to know the best technique to go about this, or how Apple have implemented it, as it is a useful pattern. (And, if I am correct, differs from a delegate pattern?).

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

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

发布评论

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

评论(1

羞稚 2024-11-15 22:59:19

这很简单。这是您可以想象的一种工作方式:(

@interface UIControlTargetActionPair
@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL action
@end

使用 @implementation 来综合这些属性)

然后:

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)events {
  if (events & UIControlEventTouchDown) {
    UIControlTargetActionPair *pair = [[UIControlTargetActionPair alloc] init];
    [pair setTarget:target];
    [pair setAction:action];
    [[self mutableArrayOfTargetActionPairsForControlEvent:UIControlEventTouchDown] addObject:pair];
    [pair release];
  }
  else if (....) {
    ...
  }
  // repeat for all relevant control events
}

删除就是简单地从适当的数组中远程远程对对象,并且发送事件只是像这样:

- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents {
  NSArray *pairs = [self mutableArrayOfTargetActionPairsForControlEvent:controlEvents];
  for (UIControlTargetActionPair *pair in pairs) {
    [[pair target] performSelector:[pair action] withObject:self withObject:theCurrentEvent];
  }
}

当然,这不是实际的代码。但代码就在我面前,这就是一般原则。 :)

It's pretty simple. Here's one way you could imagine it working:

@interface UIControlTargetActionPair
@property (nonatomic, assign) id target;
@property (nonatomic, assign) SEL action
@end

(with an @implementation to just synthesize those properties)

Then:

- (void)addTarget:(id)target action:(SEL)action forControlEvents:(UIControlEvents)events {
  if (events & UIControlEventTouchDown) {
    UIControlTargetActionPair *pair = [[UIControlTargetActionPair alloc] init];
    [pair setTarget:target];
    [pair setAction:action];
    [[self mutableArrayOfTargetActionPairsForControlEvent:UIControlEventTouchDown] addObject:pair];
    [pair release];
  }
  else if (....) {
    ...
  }
  // repeat for all relevant control events
}

Removal would be to simply remote the pair object from the appropriate array, and sending the events would simply be something like this:

- (void)sendActionsForControlEvents:(UIControlEvents)controlEvents {
  NSArray *pairs = [self mutableArrayOfTargetActionPairsForControlEvent:controlEvents];
  for (UIControlTargetActionPair *pair in pairs) {
    [[pair target] performSelector:[pair action] withObject:self withObject:theCurrentEvent];
  }
}

Of course, that's not the actual code. But having the code here in front of me, it is the general principle. :)

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