views:

607

answers:

3

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

A: 

Yes, the superview is the view, that contains yours. Your view shouldn't know which exactly is its view controller, because that will break the 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 pointer to your view, you should have pointer to your controller, which in turn can either execute some controlling logic, or pass something to its view.

Dimitar Dimitrov
I'm not sure if it would break MVC principles. At any one point, a view has only one view controller. Being able to get to it in order to pass a message back to it, should be an automatic feature, not one where you have to work to achieve (by adding a property to keep track). One could say the same thing about views: why do you need to know who child you are? Or whether there are other sibling views. Yet there are ways to get those objects.
mahboudz
You are somewhat right about the view, knowing about its parent, it's not super-clear design decision, but it's already established to do some actions, using directly a superview member variable (check parent type, remove from parent, etc). Having worked with PureMVC recently, I have become a little more nit-picky about design abstraction :) I would make parallel between iPhone's UIView and UIViewController classes and PureMVC's View and Mediator classes - most of the time, the View class doesn't need to know about its MVC handler/interface (UIViewController/Mediator).
Dimitar Dimitrov
+1  A: 

I think this thread has the answers:

Get to UIViewController from UIView on iPhone?

Ushox
A: 

I think you can propagate the tap to the view controller and let it handle it. This is more acceptable approach. As for accessing a view controller from its view, you should maintain a reference to a view controller, since there is no another way. See this thread, it might help: Accessing view controller from a view

Nava Carmon
If you have a handful of views, and one closes all the views, and you need to call viewWillDisappear, wouldn't it be easier for that view to detect the tap than to hand the tap to the view controller and have the view controller check with all the views to see which one was tapped on?
mahboudz