Skip to content

Instantly share code, notes, and snippets.

@vilanovi
Created May 26, 2014 15:11
Show Gist options
  • Save vilanovi/cc0a35d86d298ed126d7 to your computer and use it in GitHub Desktop.
Save vilanovi/cc0a35d86d298ed126d7 to your computer and use it in GitHub Desktop.
Find First Responder by asking UIApplication
// ************************************************************* //
// UIResponder+FirstResponder.h
// ************************************************************* //
@interface UIResponder (FirstResponder)
+ (UIResponder*)firstResponder;
@end
// ************************************************************* //
// UIResponder+FirstResponder.m
// ************************************************************* //
static __weak UIResponder *__currentFirstResponder = nil;
@implementation UIResponder (FirstResponder)
+ (UIResponder*)firstResponder
{
__currentFirstResponder = nil;
// Apple Documentation: "If target is nil, the app sends the message to the first responder"
[[UIApplication sharedApplication] sendAction:@selector(mjz_findFirstResponder:) to:nil from:nil forEvent:nil];
return __currentFirstResponder;
}
- (void)mjz_findFirstResponder:(id)sender
{
__currentFirstResponder = self;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment