Created
May 26, 2014 15:11
-
-
Save vilanovi/cc0a35d86d298ed126d7 to your computer and use it in GitHub Desktop.
Find First Responder by asking UIApplication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ************************************************************* // | |
// 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