iPhone:UIView 对象如何知道哪个是它的控制器?
响应者链将事件发送到视图,然后发送到其控制器。但是系统如何知道视图的控制器对象呢?我在 UIView 中没有找到任何存储对其控制器的引用的属性。另一方面,控制器显然具有 view 属性来标识其视图。
The responder chain sends an event to a view and then to its controller. But how does the system know the controller object for the view ? I didn't find any property in UIView that stores the reference to its controller. On the other hand, a controller clearly has the view property to identify its view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
UIView
(UIResponder
子类)通过实例方法知道它的viewcontroller或superview- (UIResponder *)nextResponder
来自
UIResponder
类参考:UIView
(UIResponder
subclass) knows about it's viewcontroller or superview through the instance method- (UIResponder *)nextResponder
From
UIResponder
class reference:据我了解,ViewController是系统的第一个对象(不是UIView),它包含在响应者链中(阅读UIResponder类),并管理触摸,将它们发送到相应的UIView。
As I understand, ViewController is the first object for system (not the UIView), it is included in responder chain (read about UIResponder class), and manages touches, sending them to corresponding UIView's.
以下是响应者链如何工作的一个很好的解释: http:// iphonedevelopment.blogspot.com/2008/10/bit-about-responder-chain.html。
编辑:
因为视图控制器与它们管理的视图紧密绑定,所以它们也是用于处理事件的响应者链的一部分。视图控制器本身是 UIResponder 类的后代,并插入到托管视图及其超级视图之间的响应程序链中。
因此 UIView 和 UIViewControllers 也是 UIResponder 的后代,它有一个 setNextResponder: 方法来跟踪链中的下一个响应者。
莫西
Here is a good explanation how the responder chain works: http://iphonedevelopment.blogspot.com/2008/10/bit-about-responder-chain.html .
EDIT:
Because view controllers are tightly bound to the views they manage, they are also part of the responder chain used to handle events. View controllers are themselves descendants of the UIResponder class and are inserted into the responder chain between the managed view and its superview.
So the UIView and UIViewControllers as well are descendants of UIResponder, which has a setNextResponder: method to keep track of the next responder in the chain.
Moszi