mahboudz mahboudz - 4 months ago 8
iOS Question

Given a view, how do I get its viewController?

I have a pointer to a

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

Answer

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.

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.