0

Okay i have two urtext fields, when i click on one of them, the keyboard shows.

now i have another button so when the user clicks it, i want to hide the keyboard (okay it is a stupid design, but i am just learning).

when the user clicks that button, what i do in code is calling the resignfirstresponder method on both of the ui text fields like this:

self.firstuitextfield.resignFirstResponder()
self.seconduitextfield.resignFirstResponder()

and by some maject the keyboard disappear, my question is why is that? I just removed the first responder from them, and that means, according to this question, they will not receive the messages first anymore. How can this make the keyboard disappear? and bty who now becomes the first responder?

Community
  • 1
  • 1
Marco Dinatsoli
  • 10,322
  • 37
  • 139
  • 253

1 Answers1

1

Nobody is the first responder! That's what's happening! And if they're no responders, then the keyboard hides! Pretty nifty, right? But if you want one line of code to do this, try self.view.endediting(true), instead.

MQLN
  • 2,292
  • 2
  • 18
  • 33
  • i know about the end editing method. so are you saying that the keyboard hides because there is no first responder? though i didn't send any message to hide the keyboard – Marco Dinatsoli Nov 06 '15 at 02:20
  • Yup! That's exactly what I'm saying. How were you interpreting the methods you linked to? Their purpose is just to determine whether or not a view can become a first responder. – MQLN Nov 06 '15 at 02:21
  • In this same way you cal call the keyboard by calling the 'becomeFirstResponder()' method! – MQLN Nov 06 '15 at 02:22
  • according to your answer, the keyboard diappeass because there is no first responder anymore. now i called the resign first responder method just on one of the two text fields, and they keyboard disappears too, why? there is still the second text field as the first responder – Marco Dinatsoli Nov 06 '15 at 02:25
  • Yes, that's correct! There can only be one first responder at a time. So if you resign the first responder from the current first responder, that'll trigger the keyboard to hide. If you're curious on finding out which subview is your first responder, try using this extension, and then making the first responder backgroundcolor a different color or something! https://gist.github.com/mbigatti/807a65dfb09f7b442585 – MQLN Nov 06 '15 at 02:28
  • Try resigning the first responder form each separately, and only 1 should cause the keyboard to hide. I guess you just used the one that is currently the first responder! – MQLN Nov 06 '15 at 02:30