You must not forget about UIViewControllersThe first responder can be any instance of the class UIResponder, so there are other classes that maymight be asthe first responder despite the UIViews. For example UIViewController might also be the FirstResponderfirst responder.
For that reason,In this gist you may loop intowill find a recursive way to get the UIViewController hierarchy, as well as intofirst responder by looping through the UIView hierarchy of controllers starting from the rootViewController of the application's windows.
You can retrieve then the first responder by doing
@implementation UIView (FindFirstResponder) - (UIView *)findFirstResponder { if (self.isFirstResponder) { return self; } for (UIView *subView in self.subviews) { UIView *firstResponder = [subView findFirstResponder]; if (firstResponder != nil) { return firstResponder; } } return nil; } @end @implementation UIViewController (FindFirstResponder) - (idvoid)findFirstResponder {foo if (self.isFirstResponder) { return self; // Get the first } responder id firstResponder = [self.view findFirstResponder]; if (firstResponder != nil) { return firstResponder; } for (UIViewController *childViewController in self.childViewControllers) { firstResponder = [childViewController[UIResponder findFirstResponder];firstResponder]; if (firstResponder != nil) { // Do whatever returnyou firstResponder;want } [firstResponder resignFirstResponder]; } return nil; } @end SoHowever, nowif the first responder is not a subclass of UIView or UIViewController, in your method youthis approach will fail.
To fix this problem we can do a different approach by creating a category on UIResponder and perform some magic swizzeling to be able to build an array of all living instances of this class. Then, to get the first responder simply by doing:we can simple iterate and ask each object if -isFirstResponder.
- (void)foo { UIWindow *mainWindow = [[UIApplication sharedApplication] keyWindow]; UIResponder *firstResponder = [mainWindow.rootViewController findFirstResponder]; // Do what you want with your firstResponder } This approach can be found implemented in this other gist.
Hope it helps.