Is There Any Way of Asking an iOS View Which of Its Children Has First Responder Status

Is there any way of asking an iOS view which of its children has first responder status?

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.

Get the current first responder without using a private API

In one of my applications I often want the first responder to resign if the user taps on the background. For this purpose I wrote a category on UIView, which I call on the UIWindow.

The following is based on that and should return the first responder.

@implementation UIView (FindFirstResponder)
- (id)findFirstResponder
{
if (self.isFirstResponder) {
return self;
}
for (UIView *subView in self.subviews) {
id responder = [subView findFirstResponder];
if (responder) return responder;
}
return nil;
}
@end

iOS 7+

- (id)findFirstResponder
{
if (self.isFirstResponder) {
return self;
}
for (UIView *subView in self.view.subviews) {
if ([subView isFirstResponder]) {
return subView;
}
}
return nil;
}

Swift:

extension UIView {
var firstResponder: UIView? {
guard !isFirstResponder else { return self }

for subview in subviews {
if let firstResponder = subview.firstResponder {
return firstResponder
}
}

return nil
}
}

Usage example in Swift:

if let firstResponder = view.window?.firstResponder {
// do something with `firstResponder`
}

Trying to find which text field is active ios

You need to search for an object that has become a first responder. First responder object is the one using the keyboard (actually, it is he one having focus for user input). To check which text field uses the keyboard, iterate over your text fields (or just over all subviews) and use the isFirstResponder method.

EDIT:
As requested, a sample code, assuming all text fields are a subview of the view controller's view:

for (UIView *view in self.view.subviews) {
if (view.isFirstResponder) {
[self doSomethingCleverWithView:view];
}
}

What are the First Responder and Exit boxes purpose in the storyboard editor?

I've never used it and probably never will but you can assign an object to be the first in line to receive the events from the UI.

I suppose you could be creating a UIView subclass and add it in to a UIViewController but you actually want some other object to receive and process the events other than the UIViewController you are adding it to.

I found this link which kind of explains it a bit better.

UIViewController - can't become first responder

Update

As I suspected, I assumed wrong about UIViewController/firstResponder usage. This thread in the apple dev forums talks specifically about getting shaking to work.

Original Answer

Call becomeFirstResponder on the UI element that you want to respond. The events will automatically get forwarded to the UIViewController as long as no other objects in the chain implement the touches methods (or at least keep forwarding them up the chain).

Side note: To build on the comments of others, it really doesn't make sense for a UIViewController to be the "first" responder. The first responder should be an object with an on screen representation (a UIView or one of its subclasses).

Although this may be a completely incorrect statement, there may be undocumented behavior in UIViewController that prevents it from becoming the firstResponder because of these issues (Someone smarter than me may be able to verify the validity of this).

Find View that is the firstresponder

All I had to do was

@implementation UIView (FindViewThatIsFirstResponder)
- (UIView *)findViewThatIsFirstResponder
{
if (self.isFirstResponder) {
return self;
}

for (UIView *subView in self.subviews) {
UIView *firstResponder = [subView findViewThatIsFirstResponder];
if (firstResponder != nil) {
return firstResponder;
}
}

return nil;
}
@end

Get UITextField from Keyboard Notifications

This is not possible to deduce from UIKeyboardDidShowNotification simply because it contains following information only. However, check this SO thread which has a brilliant solution on how to get the current first responder.

NSConcreteNotification 0x7ff14d1ac0d0 {name =
UIKeyboardWillShowNotification; userInfo = {
UIKeyboardAnimationCurveUserInfoKey = 7;
UIKeyboardAnimationDurationUserInfoKey = "0.25";
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 676}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 460}";
UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 568}, {320, 216}}";
UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 352}, {320, 216}}";
UIKeyboardIsLocalUserInfoKey = 1; }}



Related Topics



Leave a reply



Submit