iPhone:UIView 对象如何知道哪个是它的控制器?

发布于 2024-10-10 03:54:05 字数 106 浏览 0 评论 0原文

响应者链将事件发送到视图,然后发送到其控制器。但是系统如何知道视图的控制器对象呢?我在 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 技术交流群。

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

发布评论

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

评论(3

凤舞天涯 2024-10-17 03:54:05

UIViewUIResponder子类)通过实例方法知道它的viewcontroller或superview

- (UIResponder *)nextResponder

来自UIResponder 类参考:

UIResponder类不存储
或设置下一个响应者
自动,而不是返回 nil
默认情况下。子类必须重写
此方法设置下一个响应者。
UIView通过以下方式实现这个方法
返回 UIViewController 对象
管理它(如果有的话)或其
超级视图(如果没有);
UIViewController实现该方法
通过返回其视图的超级视图;
UIWindow 返回应用程序
对象,UIApplication 返回 nil。

UIView (UIResponder subclass) knows about it's viewcontroller or superview through the instance method

- (UIResponder *)nextResponder

From UIResponder class reference:

The UIResponder class does not store
or set the next responder
automatically, instead returning nil
by default. Subclasses must override
this method to set the next responder.
UIView implements this method by
returning the UIViewController object
that manages it (if it has one) or its
superview (if it doesn’t);
UIViewController implements the method
by returning its view’s superview;
UIWindow returns the application
object, and UIApplication returns nil.

眼眸里的那抹悲凉 2024-10-17 03:54:05

据我了解,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.

风情万种。 2024-10-17 03:54:05

以下是响应者链如何工作的一个很好的解释: 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

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