Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does UIView nextResponder know what the UIViewController is?

Just as a matter of curiosity, how does the nextResponder method implementation of the UIView class know who is the UIViewController that manages the view? The UIResponder documentation states this, and I can see it work, but I don't quite understand it. To the best of my knowledge, a UIView does not maintain a reference to it's controller, so what's happening behind the scenes? Or am I just missing something obvious?

I'm still very new to Objective-C and iPhone development so I apologize if this is something obvious, but I am quite curious.

Thanks!

like image 876
Fin Avatar asked Oct 19 '09 05:10

Fin


People also ask

What is a UIViewController?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

What is the view controller responsible for view?

If you need to find the view controller that is responsible for a particular view, the easiest thing to do is walk the responder chain. This chain is built into all iOS apps, and lets you walk from one view up to its parent view, its grandparent view, and so on, until it reaches a view controller.

What is a UIResponder?

An abstract interface for responding to and handling events.

What is the responder chain in swift?

The responder chain is the series of events that happen once we start interacting with the application on the iPhone. As an example, whenever we tap on an UITextfield, the whole series of responder initiates. It happens because of elements like UIView, UIViewController, UIApplication subclass UIResponder.


2 Answers

The responder chain is separate from the view hierarchy. The responder chain might look like this:

First Responder > View Hierarchy > Window > Window Delegate > etc...

However, objects can insert themselves into the responder chain and that's what UIViewController does. From the docs:

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.

In Big Cocoa, this is accomplished using the -setNextResponder: method. That method isn't public in Cocoa Touch, but nevertheless that's what UIViewController appears to do.

like image 52
Darren Avatar answered Oct 03 '22 21:10

Darren


If you look at UIView.h, you can see a member variable named _viewDelegate that is of type UIViewController*, this is probably where the view controller reference is stored when the viewcontroller's view property is set, and where it knows to look when you call nextResponder.

like image 35
Kevlar Avatar answered Oct 03 '22 21:10

Kevlar