将我自己的事件添加到“已发送事件”中在我自己的自定义 UIView 的 Interface Builder 菜单中
我创建了 UIView 的子类,我想让它发布显示在 Interface Builder(实际上是 Xcode4)中的自定义事件,就像 UIButton 等控件在右键单击时在“已发送事件”区域中具有一堆事件一样在 Xcode 4 设计器中的控件上。我知道我可以使用委托(通过协议)或通知(通过 UINotificationCenter)让使用我的自定义视图的对象知道某些事情何时发生,但我想知道“目标操作机制”(在Cocoa 基础指南)适合/需要/可能使用并与 Xcode 设计器集成。由于主要来自 .NET 背景,这种方法似乎与 .NET 事件模型密切相关,并且对我来说最有意义。
I have created a subclass of UIView and I would like to have it publish custom events that show up in Interface Builder (actually Xcode4) the same way that controls like UIButton have a bunch of events in the "Sent Events" area when you right click on a control in the Xcode 4 designer. I know I can use Delegation (via Protocols) or Notification (via the UINotificationCenter) to let the objects using my custom view know when certain things happen, but I would like to know if the "The Target-Action Mechanism" (described in the Cocoa Fundamentals Guide) is appropriate/desirable/possible to use and be integrated with the Xcode designer. Coming from a mostly .NET background, this approach seems to be closely related to the .NET event model and makes the most sense to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
有 UIControlEventApplicationReserved,它为您提供应用程序可以使用的一系列事件标识符。但是,我认为没有任何方法可以告诉 Interface Builder 有关应用程序定义的事件的信息,因此您在 IB 中的事件不会获得与 UIControl 的标准事件相同的支持。相反,您必须在代码中为每个应用定义的事件指定目标和操作。 (如果我在这一点上弄错了,请有人纠正我。)这并不困难,但有点不同。
There's UIControlEventApplicationReserved, which gives you a range of event identifiers that your app can use. However, I don't think there's any way to tell Interface Builder about application-defined events, so you won't get the same support for your events in IB as you find for UIControl's standard events. Instead, you'll have to specify the target and action for each app-defined event in code. (Please, someone correct me if I'm mistaken on this point.) That's not at all difficult, but it is a little different.
一个简单的方法是扩展
UIControl
而不是UIView
这将允许您向所有默认事件添加目标(与UIButton
相同) > 等)。注意:为了让我的自定义
UIControl
处理事件而不是我在其上分层的控件,我必须确保在所有控件上设置userInteractionEnabled = NO
分层控件。A simple way to do this is to extend
UIControl
instead ofUIView
this will allow you to add a target to all the default events (same asUIButton
etc).Note: in order for my custom
UIControl
to handle the events as opposed to the controls I layered on top of it I had to ensure thatuserInteractionEnabled = NO
was set on all the layered controls.您可以使用 IBOutletCollection 来完成此操作。这种方法的优点是您可以在 Interface Builder 中链接对象。缺点是您无法直接链接到 IBActions(如 UIControl)。这是使用协议的干净实现:
ObserverProtocol.h
MyObject.h
MyObject.m
然后,您只需在要观察事件的类中实现 ObserverProtocol(当然还要在 Interface Builder 中进行绑定)。
You can do this using an IBOutletCollection. The advantage of this approach is that you can link objects in Interface Builder. The downside is that you can't link directly to IBActions (like UIControl). Here is a clean implementation using a protocol:
ObserverProtocol.h
MyObject.h
MyObject.m
Then you just need to implement ObserverProtocol in classes you want to observe the event (and to do the binding in Interface Builder of course).