Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Walking the responder chain to pass custom events. Is this wrong?

According to the iOS documentation, the responder chain is used to pass touch events "up the chain". It's also used for actions generated by controls. Fine.

What I really would like to do is send a custom event "up the chain". The first responder to pick up on the event will handle it. This seems like a pretty common pattern, but I can't find any good explanation on how to do it the "iOS/Cocoa way".

Since the responder chain is exactly what I need, I came up with a solution like this:

// some event happened in my view that 
// I want to turn into a custom event and pass it "up":

UIResponder *responder = [self nextResponder];

while (responder) {

   if ([responder conformsToProtocol:@protocol(ItemSelectedDelegate)]) {
       [responder itemSelected:someItem];
       break;
   } 

   responder = [responder nextResponder];
}

This works perfectly, but I have a feeling that there should be other ways of handling this. Walking the chain manually this way doesn't seem very... nice.

Note that notifications are not a good solution here, because I only want the objects in the view hierarchy to be involved, and notifications are global.

What's the best way of handling this in iOS (and Cocoa for that matter)?

EDIT:

What do I want to accomplish?

I have a view controller, which has a view, which has subviews etc... Several of the subviews are of a specific type that show an item from the database. When the user taps this view, a signal should be sent to the controller to navigate to a detail page of this item.

The view that handles the tap is several levels below the main view in the view hierarchy. I have to tell the controller (or in some cases a specific subview "up the chain") that an item was selected.

Listening to notifications would be an option, but I don't like that solution because selecting an item is not a global event. It's strictly tied to the current view controller.

like image 857
Philippe Leybaert Avatar asked Oct 05 '10 12:10

Philippe Leybaert


People also ask

How does the responder chain work?

The responder chain allows responder objects to transfer responsibility for handling an event or action message to other objects in the application. If an object in the responder chain cannot handle the event or action, it resends the message to the next responder in the chain.

What is regular expression and responder chain?

Regular Expression – These are the special string patterns that describe how a search is performed through a string. Responder Chain – It is a hierarchy of objects that obtain the opportunity to respond to the events.

What is a responder in swift?

A responder object is any instance of the UIResponder class, and common subclasses include UIView , UIViewController , and UIApplication . Responders receive the raw event data and must either handle the event or forward it to another responder object.

What is UIResponder in IOS?

An abstract interface for responding to and handling events.


2 Answers

UIApplication has a method for just this purpose, as does its Cocoa cousin. You can replace all of that code in your question with one message.

like image 64
Peter Hosey Avatar answered Sep 27 '22 22:09

Peter Hosey


You're pretty close. What would be more standard is something like this:

@implementation NSResponder (MyViewController)
- (void)itemSelected:(id)someItem
{
    [[self nextResponder] itemSelected:someItem];
}
@end

That's generally how events get passed up the chain by default. Then in the right controller, override that method to instead take a custom action.

This may not be the right pattern for what you want to achieve, but it is a good way to pass messages up the responder chain.

like image 35
Mike Abdullah Avatar answered Sep 27 '22 20:09

Mike Abdullah