在Objective C中获取firstresponder [英] Get firstresponder in Objective C
问题描述
我在尝试找出哪个 UITextField
是当前的第一响应程序时遇到问题。
I am having trouble trying to figure out which UITextField
is the current First Responder.
如果用户点击特定的 UITextField
,我想要做的是设置一个布尔值。所以要这样做,我需要能够告诉这个特定的文本字段是否已成为第一个响应者。
What I am trying to do is a set a boolean value if the user clicks in a particular UITextField
. So to do that I need to be able to tell if this particular text field has become the first responder.
我知道如何设置第一个响应者,但不知道如何以确定字段是否实际上已成为第一个响应者。
I know how to set the first responder but just not sure how to tell if a field has actually become the first responder.
推荐答案
[...]但不知道如何判断字段是否
第一响应者。
[...] but just not sure how to tell if a field has actually become the first responder.
UIView
继承 isFirstResponder
方法从 UIResponder
,它告诉你这个。
UIView
inherits the isFirstResponder
method from UIResponder
which tells you this.
找到第一个响应者的最简单方法是不检查单个控件/视图,并且没有多种方法可用于所有不同类型的可能的第一响应者。类别 UIView
其中添加 findFirstResponder
方法:
The easiest way to find whatever the first responder is without checking individual controls/views and without multiple methods for all of the different types of possible first responders is to just make a category on UIView
which adds a findFirstResponder
method:
UIView + FirstResponder.h
#import <UIKit/UIKit.h>
@interface UIView (FirstResponder)
- (UIView *)findFirstResponder;
@end
UIView + FirstResponder.m >
UIView+FirstResponder.m
#import "UIView+FirstResponder.h"
@implementation UIView (FirstResponder)
- (UIView *)findFirstResponder
{
if ([self isFirstResponder])
return self;
for (UIView * subView in self.subviews)
{
UIView * fr = [subView findFirstResponder];
if (fr != nil)
return fr;
}
return nil;
}
@end
这篇关于在Objective C中获取firstresponder的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!