Ios 如何知道哪个UITextField正在响应?

Ios 如何知道哪个UITextField正在响应?,ios,objective-c,uitextfield,uitextview,Ios,Objective C,Uitextfield,Uitextview,在UIViewController中,我将自己添加为观察者: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWill

UIViewController
中,我将自己添加为观察者:

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];

    ......
}

- (void)keyboardWillShow:(NSNotification *)notification {

    NSDictionary *userInfo = [notification userInfo];
    NSValue* aValue = [userInfo objectForKey:UIKeyboardFrameEndUserInfoKey];
    CGRect keyboardRect = [aValue CGRectValue];

    if (textField1.isFirstResponder) {
        NSLog(@"I am here1");
    }

    if (textField2.isFirstResponder) {
        NSLog(@"I am here2");
    }

     ........

}

我还在控制器中创建了五个
UITextField
。我的问题是如何知道调用哪个文本字段。我试着用这个方法来找出谁的“isFirstResponder”发生了变化,但它不起作用。。。为什么不使用
UITextFieldDelegate的
-textfieldDiBeginediting:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"%@",textField);
}

嗯。。。为什么不使用
UITextFieldDelegate的
-textfieldDiBeginediting:

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"%@",textField);
}

使用标记属性!!当您创建文本字段时,请设置它们的标记(设置为特定于文本字段的数字),当您收到通知时,只需检查它们的标记

使用标记属性!!当您创建文本字段时,请设置它们的标记(设置为特定于文本字段的数字),当您收到通知时,只需检查它们的标记

尝试UIKeyboardDidShowNotification而不是UIKeyboardWillShowNotification

尝试UIKeyboardDidShowNotification而不是UIKeyboardWillShowNotification

使用notifications而不是delegate方法有合法的理由。每一个都会在不同的时间点被调用lifecycle@Cutetare:是的,有可能,但在我看来,这个问题似乎与
UIKeyboardWillShowNotification
通知(即使他的代码中包含通知)一般无关。使用Notifications而不是delegate方法有合法的理由。每一个都会在不同的时间点被调用lifecycle@Cutetare:是的,有可能,但这个问题对我来说似乎是一般性的,而不是与
UIKeyboardWillShowNotification
notification(即使他的代码中包含它)有关。FirstResponder对我来说工作得非常好。(我刚刚测试过)如果在键盘可见时更改文本字段,则不会收到通知,也不会调用方法。在单击其他文本字段之前隐藏键盘进行检查。@Ad-J我不得不说,很抱歉isFirstResponder真的在工作!我犯了一个愚蠢的错误!我犯了一个错误。我已经解决了这个问题。isFirstResponder对我来说工作得非常好。(我刚刚测试过)如果在键盘可见时更改文本字段,则不会收到通知,也不会调用方法。在单击其他文本字段之前隐藏键盘进行检查。@Ad-J我不得不说,很抱歉isFirstResponder真的在工作!我犯了一个愚蠢的错误!我犯了一个错误。我已经解决了这个问题。