mogio
mogio

Reputation: 843

Sending an event up the UIResponder chain?

I just created a custom UIViewController with some user actions like touch. I would like to handle the user interaction in the parentObject. In other words the one that created the ViewController.

From other languages I am used to use Events that are pushed up. So my parent object would have some kind of listener on the reference of the ViewController object it can react to.

How would that type of interaction handled by Objective C?

Upvotes: 1

Views: 1729

Answers (3)

hamstergene
hamstergene

Reputation: 24439

This can be done by 1) responder chain, 2) notifications and 3) delegates.

  1. All UI objects form the responder chain, starting from the currently focused element, then it's parent view and so on, usually until the application object. By sending action to the special First Responder object in your nib, you'll throw it down the responder chain until someone handles it. You can use this mechanism for firing events without knowing who and when will handle them. This is similar to HTML event model.

  2. Notifications send by NSNotificationCenter can be received by any number of listeners. This is the closest analog to e.g. C# events.

  3. Delegates is the simplest mechanism of sending event to one single object. The class declares weak property named delegate that can be assigned with any object, and a protocol that this object is supposed to implement. Many classes use this approach; the main problem is that you can't have more than one listener this way.

Upvotes: 3

Ashley Mills
Ashley Mills

Reputation: 53111

It sounds like you need to implement a delegate protocol, which will allow your 'child' view controller to communicate back to it's 'parent'

See http://developer.apple.com/library/ios/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

Upvotes: 0

Finn Larsen
Finn Larsen

Reputation: 2199

you should look into delegations/delegate for interactions between two viewControllers. You need to understand how it works first.

https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html

Upvotes: 0

Related Questions