给定一个视图,我如何获得它的 viewController? [英] Given a view, how do I get its viewController?

查看:31
本文介绍了给定一个视图,我如何获得它的 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天全站免登陆