In Mac OS X, you can find the first responder like this:
[[self window] firstResponder]
Is there any way of doing it in iOS? Or do you need to enumerate the child controls and send an isFirstResponder
message to each one?
I really like VJK's solution, but as MattDiPasquale suggests it seems more complex than necessary. So I wrote this simpler version:
UIResponder+FirstResponder.h:
#import <UIKit/UIKit.h>
@interface UIResponder (FirstResponder)
+(id)currentFirstResponder;
@end
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
import UIKit
extension UIResponder {
private static weak var _currentFirstResponder: UIResponder?
static var currentFirstResponder: UIResponder? {
_currentFirstResponder = nil
UIApplication.shared.sendAction(#selector(UIResponder.findFirstResponder(_:)), to: nil, from: nil, for: nil)
return _currentFirstResponder
}
@objc func findFirstResponder(_ sender: Any) {
UIResponder._currentFirstResponder = self
}
}
I also made it a class method since that seemed to make more sense. You can now find the first responder like so: [UIResponder currentFirstResponder]
YES
from -canBecomeFirstResponder
. This is great for input accessory views that are always on screen or handling shortcuts even when no view is focussed, etc. As such, using UIView *
would be wrong here.
Commented
May 27, 2014 at 12:54
TextField
which is in the scrollview
. After I check firstResponder
in the way described and get ScrollView as the first responder. At the same time [[[[UIApplication sharedApplication] windows] firstObject] valueForKey:@"firstResponder"]
gives TextField.
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.
-[UIApplication sendAction:to:from:forEvent:]
is neat! :) But, instead of using an associated object to reference firstResponder
, why not just use a static variable since there'll only be one firstResponder
at a time?
Commented
Jul 1, 2012 at 21:26
sendAction:
until I read the docs. That must be the only part of iOS's APIs that actually gives direct access to the first responder.
If you need first responder just so you can ask it to resign its status, here is an approach to get any to resign. UIView has a method that will iterate through all of UIViews subviews and ask any that are first responder to resign.
[[self view] endEditing:YES];
Here is a link to Apple's UIView Docs "This method looks at the current view and its subview hierarchy for the text field that is currently the first responder. If it finds one, it asks that text field to resign as first responder. If the force parameter is set to YES, the text field is never even asked; it is forced to resign."
You would need to iterate over all of the child controls and test the isFirstResponder
property. When you encounter TRUE
, break out of the loop.
UIView *firstResponder;
for (UIView *view in self.view.subviews) //: caused error
{
if (view.isFirstResponder)
{
firstResponder = view;
break;
}
}
BETTER SOLUTION
See Jakob's answer.