Mentioned in the previous article on the keyboard corresponding time to get the current first responder method is that Apple has a way to upload to the app Store, this article will introduce a very simple and unused to the private API method to obtain the current first responder.
Implementation idea: The iOS API used is
- (BOOL)sendAction:(SEL)action to:(id)target from:(id)sender forEvent:(UIEvent *)event
With this API, as long as the incoming target is set to nil, the system automatically finds the responder that responds to the action along the response chain. We only need to let all uiresponder subclasses respond to our custom action to know which object the current first responder is.
Objective-c specific implementation code. h File: uiresponder+firstresponder.h
#import <UIKit/UIKit.h>
@interface Uiresponder (FirstResponder)
+ (ID) currentfirstresponder;
@end
. m File: uiresponder+firstresponder.m
#import "Uiresponder+firstresponder.h"
Static __weak ID currentfirstresponder;
@implementation Uiresponder (FirstResponder)
+ (ID) currentfirstresponder {
Currentfirstresponder = nil;
[[uiapplication sharedapplication] sendaction:@selector(findfirstresponder:) to: Nil from:nil forevent:nil];
return Currentfirstresponder;
}
-(void) Findfirstresponder: (ID) sender {
Currentfirstresponder = self;
}
@end
Get first Responder when developing iOS