I wrote a category on UIResponder
to find the first responder
@interface UIResponder (firstResponder)
- (id) currentFirstResponder;
@end
and
#import <objc/runtime.h>
#import "UIResponder+firstResponder.h"
static char const * const aKey = "first";
@implementation UIResponder (firstResponder)
- (id) currentFirstResponder {
[[UIApplication sharedApplication] sendAction:@selector(findFirstResponder:) to:nil from:self forEvent:nil];
id obj = objc_getAssociatedObject (self, aKey);
objc_setAssociatedObject (self, aKey, nil, OBJC_ASSOCIATION_ASSIGN);
return obj;
}
- (void) setCurrentFirstResponder:(id) aResponder {
objc_setAssociatedObject (self, aKey, aResponder, OBJC_ASSOCIATION_ASSIGN);
}
- (void) findFirstResponder:(id) sender {
[sender setCurrentFirstResponder:self];
}
@end
Then in any class that derives from a UIResponder
you can get the first responder by calling
UIResponder* aFirstResponder = [self currentFirstResponder];
but remember to import the UIResponder category interface file first!
This uses documented API's so there should be no app store rejection issues.