给出一个看法,我该如何获取它的viewController? [英] Given a view, how do I get its viewController?

查看:122
本文介绍了给出一个看法,我该如何获取它的viewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个指向 UIView 的指针。如何访问其 UIViewController [self superview] 是另一个 UIView ,但不是 UIViewController ,对吗?

I have a pointer to a UIView. How do I access its UIViewController? [self superview] is another UIView, but not the UIViewController, right?

推荐答案

是的, superview 包含您的观点。您的观点不应该知道它的视图控制器究竟是哪一个,因为这将破坏MVC原理。

Yes, the superview is the view that contains your view. Your view shouldn't know which exactly is its view controller, because that would break MVC principles.

另一方面,控制器知道它负责的视图( self.view = myView ),通常,此视图将处理方法/事件委托给控制器。

The controller, on the other hand, knows which view it's responsible for (self.view = myView), and usually, this view delegates methods/events for handling to the controller.

通常,代替指向您视图的指针,您应该有一个指向您的控制器的指针,这反过来可以执行一些控制逻辑,或者传递一些内容到它的视图。

Typically, instead of a pointer to your view, you should have a pointer to your controller, which in turn can either execute some controlling logic, or pass something to its view.

这篇关于给出一个看法,我该如何获取它的viewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆