Search code examples
iosobjective-cnsnotificationcenteruikeyboard

Get UITextField from Keyboard Notifications


I am trying to get UITextField that on which the observer UIKeyboardDidShowNotification is being called.

My Observer is in appdelegate and I have several UIViewControllers that have several UITextFields in them, in short I don't know current First Responder.

How can I get the first responder inside my Method that is fired upon the keyboard notification UIKeyboardDidShowNotification?


Solution

  • 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; }}