Is it possible to get textfield assosiated with a keyboard?

There is one manual way

if ([firstName isFirstResponder]) {
    // caused due to firstName
} else if ([lastName isFirstResponder]) {
    // caused due to lastName
}

Swift

if firstName.isFirstResponder { // caused due to firstName } 
else
if lastName.isFirstResponder { // caused due to lastName }

There is much better way to do this via did begin editing notifications of UITextField and UITextView:

UITextFieldTextDidBeginEditingNotification

and

UITextViewTextDidBeginEditingNotification

- (void)startListeningForKeyboardNotification {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(responderDidBeginEditing:) name:UITextFieldTextDidBeginEditingNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(responderDidBeginEditing:) name:UITextViewTextDidBeginEditingNotification object:nil];
}

- (void)responderDidBeginEditing:(NSNotification *)notification {
    if ([notification.object isKindOfClass:[UITextField class]]) {
        UITextField *textField = notification.object;
        // Do something with text field
    } else if ([notification.object isKindOfClass:[UITextView class]]) {
        UITextView *textView = notification.object;
        // Do something with text view
    }
}

The easiest (and almost only) way to do this happens to also be the best way (as @iphonic mentioned)

Implement the UITextFieldDelegate delegate method textFieldShouldBeginEditing:textField

This method will be called before any other event (including any keyboard notifications)

Store the UITextField that received this delegate call to be referenced when determining which field triggered this event.


What I also recommend doing is, if you are looking to determine which keyboard fired the current keyboard appearance, within this delegate method simply register for receiving keyboard notifications.

- (BOOL)textFieldShouldBeginEditing:(UITextView *)textView {

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

    return YES;

}

This way, you can be certain that when keyboardWillShow: is called, it was in fact triggered from this specific text field.

Of course, to make sure you keep track of your text fields correctly, stop listening when the text field stops editing.

- (void)textFieldDidEndEditing:(UITextView *)textView {

    [NSNotificationCenter.defaultCenter removeObserver:self name:UIKeyboardWillShowNotification object:nil];

}

Let me emphasize that @iphonic comment is the right way to go.

You should use UITextField delegate function textFieldShouldBeginEditing:

Anything short of that is a kludge: UIKeyboardWillShowNotification assumes the software keyboard will show up, with is a very dangerous assumption and is likely to fail in all sorts of situations, starting with but not limited to Bluetooth keyboards. Try cmd-K in Simulator.

Here is aforementioned kludge, inspired by Get the current first responder without using a private API

func keyboardWillShow() {
    let firstResponder = self.findFirstResponder(inView: self.view)
    println("keyboardWillShow for \(firstResponder)")
}

func findFirstResponder(inView view: UIView) -> UIView? {
    for subView in view.subviews as! [UIView] {
        if subView.isFirstResponder() {
            return subView
        }

        if let recursiveSubView = self.findFirstResponder(inView: subView) {
            return recursiveSubView
        }
    }

    return nil
}