您可以使用 UIControlEventApplicationReserved 创建自定义事件吗?
我编写了一个 UIControl 的子类,它跟踪许多我感兴趣的手势。在 UIControlEvents 枚举的文档中,它说有一系列名为 UIControlEventApplicationReserved 的事件号“可供应用程序使用”。这是否意味着我可以自由地将这个范围的数字用于我自己的自定义事件?
如果是这样,有人可以告诉我如何触发事件吗?我能想到的最明显的方法是:
enum {
...
MyCustomEvent = 65,
...
};
...
UIEvent* customEvent;
...
for (id target in [self allTargets])
{
for (NSString* action in [self actionsForTarget:target forControlEvent:MyCustomEvent])
{
[self sendAction:NSSelectorFromString(action) to:target forEvent:customEvent];
}
}
这有效吗?
I have written a subclass of UIControl that tracks a number of gestures that are of interest to me. In the documentation for the UIControlEvents enumeration, it says that there is a range of event numbers called UIControlEventApplicationReserved that is "available for application use." Does this mean that I am free to use this range of numbers for my own custom events?
If so, can someone please tell me how to fire events? The obvious way I can think of to do it is this:
enum {
...
MyCustomEvent = 65,
...
};
...
UIEvent* customEvent;
...
for (id target in [self allTargets])
{
for (NSString* action in [self actionsForTarget:target forControlEvent:MyCustomEvent])
{
[self sendAction:NSSelectorFromString(action) to:target forEvent:customEvent];
}
}
Would that even work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这是一个老话题,但我要添加我的答案。尽管我对此表示怀疑,但我无法确定您是否可以在自己的应用程序中使用此掩码。
但我可以肯定地告诉你如何使用它。对于初学者,此值屏蔽位置 24、25、26 和 27 处的位。您应该编写自己的仅使用此位的枚举,例如:
完成后,您可以注册这些操作:
每次操作 MyPrimaryActionEvent被触发时,self 将收到消息 someAction:。现在如何触发该操作取决于按钮本身。在您自己的 UIControl 子类中,您可以按如下方式触发更改:
这会将所有操作发送到为 MyPrimaryActionEvent 事件注册的所有目标。你就完成了。
Okay, this is an old subject but I'm going to add my answer to this. I can't really tell for sure whether you can use this mask for your own application even though I suspect it.
But I can tell you for sure how to use it. For starter this value masks the bits at position 24, 25, 26 and 27. You should write an enum of your own that uses this bits only, for example:
Once that is done you can register for these actions:
Every time the action MyPrimaryActionEvent is triggered, self will receive the message someAction:. Now how to trigger that action is up to the button itself. In your own UIControl subclass you can trigger the change as follow:
This will send all the actions to all the targets registered for MyPrimaryActionEvent event. And you're done.