在Cocoa,UIResponder中从子视图发送消息到超级视图?
我有所有这些触摸敏感的子视图,我想从子视图向超级视图发送一条消息,说明用户选择了它,以便超级视图可以与控制器的其余部分进行通信。
我无法在子视图和控制器之间进行通信, 子视图>>超级视图>>控制器
也许使用 UIResponder 来实现这一点?
I have all these subview which are touch sensitive, I want to send a message from the subview to the superview, to say that a user selected it, so the superview can communicate with the rest of the controller.
I can not communicate between the subviews and the controllers,
subviews >> superview >> controller
Perhaps use, UIResponder to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据子视图,目标/操作机制可能对此很有用。如果您可以从 UIControl 派生子视图,那么让您的子视图将其操作消息发送到其目标(通常是您的视图控制器)就特别容易。您将能够在 Interface Builder 中布置视图,并通过将它们连接到视图控制器来指定它们的目标和操作。如果由于某种原因无法从 UIControl 派生它们,那么您必须实现目标/操作的等效项,并且在 IB 中不会获得相同的支持,但它仍然非常简单。
另一种可能性是让视图控制器对所有子视图进行触摸处理。正如您所建议的,这基本上是利用响应者链,但是是在触摸处理级别。如果有很多子视图需要跟踪,这可能并不理想,但它是可行的。
第三种方法是让您的子视图在被选择时发布通知。
事实上,UIResponder 不提供沿着响应者链传递任意消息的机制。我不确定添加该功能是否是专门从子视图向视图控制器发送消息的最优雅的方式。视图控制器和子视图之间可能存在许多中间对象,当您已经知道消息要发送到哪里时,涉及整个链似乎是错误的。然而,考虑扩展 UIResponder 以使响应者链成为不仅仅是事件的渠道是很有趣的。您可以向 UIResponder 添加一个类别:
警告 上面的代码完全未经测试,并且由于我尚未想到的原因,可能是一个糟糕的想法。谨慎行事。预计会出现编译错误。交叉手指。如果效果很好请告诉我,如果效果不好请别打扰我。
Depending on the subviews, the target/action mechanism can be good for this. If you can derive your subviews from UIControl, then it's particularly easy to have your subviews send their action message to their target, which is usually your view controller. You'll be able to lay out your views in Interface Builder and specify their target and action by connecting them to the view controller. If you can't derive them from UIControl for some reason, then you'd have to implement the equivalent of target/action and you won't have the same support in IB, but it's still pretty straightforward.
Another possibility is to let the view controller do the touch handling for all the subviews. This is basically taking advantage of the responder chain as you suggested, but at the touch-handling level. It may not be ideal if there are a lot of subviews to keep track of, but it's workable.
A third way to do it is to have your subviews post a notification when they're selected.
As is, UIResponder doesn't provide a mechanism for passing arbitrary messages along the responder chain. I'm not sure that adding that capability is the most elegant way to send a message specifically from a subview to the view controller. There are potentially many intermediate objects between the view controller and the subviews, and involving the entire chain when you already know where you want the message to go seems wrong. However, it's interesting to think about extending UIResponder to make the responder chain a conduit for more than just events. You could add a category to UIResponder:
WARNING The code above is completely untested, and may be a lousy idea for reasons I haven't yet thought of. Proceed with caution. Expect compilation errors. Cross your fingers. Please let me know if it works out well, and leave me alone if it doesnt.
为什么不能使用
[self.superview sendMessage]
?Why can't you use
[self.superview sendMessage]
?