Custom iPhone class / object in the responder chain
I am looking for strategies, best practices and solutions for adding a custom class to a responder chain. This was because I realized that I was tackling sensory events in the same way in several different applications. To make life easier, I wanted to move the functionality to a custom class and this class became the first responder for touch events. Since my first few ideas didn't work, I figured this wouldn't be an ad-hoc problem that I could solve.
I have made several attempts based on different docs, posts, etc. that I read (which is why I am not posting the source right now). My latest attempt comes from a UIResponder and has a UIView member that holds a pointer to the current view.
Before I spent too much time "figuring out," I wondered if anyone had any ideas.
So my question is, "How do I add a custom class as a first responder, especially to receive touch events"?
Your solution sounds weird because UIView is itself a UIResponder.
If you have some generic event handling code, why not declare a category in a UIView or UIResponder so that you can then be accessed from all your UIView (or UIResponder) subclasses, including UIApplication, UIWindow, and UIControl.
a source to share
Using Roger's suggestion, the solution I'm using looks like this:
Declaration (touchmyself.h)
#import <UIKit/UIViewController.h>
@interface UIViewController (TouchMyself)
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event;
- (void) touchesMoved: (NSSet *) touches withEvent: (UIEvent *) event;
- (void) touchesEnded: (NSSet*) touches withEvent: (UIEvent*) event;
- (void) touchesCancelled: (NSSet*)touches withEvent: (UIEvent*) event;
@end
Implementation (touchmyself.m)
#import "touchmyself.h"
@implementation UIViewController (TouchMyself)
- (void) touchesBegan: (NSSet *) touches withEvent: (UIEvent *) event
{
// Do Stuff
}
...
@end
Consumer ads (some_view_controller.h)
@interface ViewSwitchBox : UIViewController
{
// Declare Stuff
}
After reading a couple of documents related to categories, chapter 1.5 "Developing the iPhone SDK" read: "
-isdi-