2

I am new to stackoverflow and iOS app development using Swift. When I want to dismiss the keyboard in my apps I connect the uitextfield in viewcontroller.swift and create an action with the event DidEndOnExit. Then in that method I call the self.resignFirstResponder() method. This is what everybody says I should do. Here is my code in viewcontroller

 @IBAction func dismissKeyboard(sender: AnyObject) {

        self.resignFirstResponder()

}

Now when I was searching to find out what this method does I found that when I choose the textfield then the keyboard appears and that textfield becomes the first responder and by calling this method it resigns the first responder and the keyboard disappears. The thing is that if I don't use this method the keyboard still disappears. All I need to do is to create the action with the DidEndOnExit event. Am I missing something here? Here is the code without the method;

 @IBAction func dismissKeyboard(sender: AnyObject) {

        //self.resignFirstResponder()

}

If someone can enlighten me that would be great! :)

Thanks in advance!

Kevin
  • 16,696
  • 7
  • 51
  • 68
  • The first responder is the UITextField not the keyboard. Do you have a `textFieldShouldReturn()` function? – somtingwong Jul 17 '15 at 17:19
  • Yes you are right. I didn't write it correctly. I will edit my question. No I don't have a textFieldShouldReturn(). – Michael Kornelakis Jul 17 '15 at 17:27
  • http://stackoverflow.com/questions/274319/how-do-you-dismiss-the-keyboard-when-editing-a-uitextfield looks like having a `DidEndOnExit` event is basically the same as `resignFirstResponder` – somtingwong Jul 17 '15 at 17:32

2 Answers2

0

maybe you enabled "Auto-enable Return Key" in the Interface builder?

Lennet
  • 413
  • 3
  • 10
0

The only thing that could come to mind is that you have an auto enabled return key. You also said it wasn't enabled, so maybe it has something to do with the rest of your code in the ViewController. Also, check any properties that you could have been playing with to get it to work, like you said you are new to swift so you we probably playing around with getting the keyboard to dismiss.

Sometimes Xcode can be a little buggy, if you can't solve the problem or get the right answer my advice is make a new project and add just the keyboard dismissing code in.

I hope this helps you out a little not seeing the rest of the code, screen shots and etc. make it so I can only give my thoughts or past experiences!

Good luck! Have fun programming in swift.

Inability
  • 26
  • 3
  • First of all thank you for your effort. I have created a new project and the only code is the code I gave at the first place. I use Xcode 6.4, if someone could make a new project and add a textfield and then create an action with didendonexit event so we could check this, that would be great. There is no need for screenshots or further info because there isn't anything else to show!!! :) – Michael Kornelakis Jul 17 '15 at 19:17
  • And you still got the same result? Interesting, Have you tried reseting your simulator? Maybe, it was a bug, anyway I'll check it out right now and let you know what I get! – Inability Jul 17 '15 at 19:27
  • Have you tried it this way? Without the self.resignFirstResponder()? – Michael Kornelakis Jul 17 '15 at 19:30
  • yes I have and I haven't gotten the same problem you have. The keyboard will just stay exactly as it is. – Inability Jul 17 '15 at 19:33
  • I don't understand this...It's very weird. I thought this could be an Xcode bug? I ll keep searching for this for a while. I could upload a youtube video... – Michael Kornelakis Jul 17 '15 at 19:36
  • Okay, I have tried to reproduce your error and couldn't get it, again I know you are rather new to swift so here were my suggestions for exiting a keyboard. Both very simple and I am sure you have seen them before! Try using touchesBegan, a simple touch will make the keyboard close, or like what you did before textFieldShouldReturn. Either of those worked. I know you already are using the one you made but this one isn't buggy? if you want to call it that? I would just get rid of your code and use this in its stead it might be easier – Inability Jul 17 '15 at 19:38
  • I don't have a problem exiting the keyboard. I can exit it WITHOUT using the resignFirstResponder() method so I can't understand why should I use it in didendonexit.... – Michael Kornelakis Jul 17 '15 at 19:40
  • I understand that >< but it can be buggy later on try using a different way of doing so because also like the comment said earlier to your question the two events are the same. So if its working then you really don't need it right? DidEndOnExit will do the same thing. I suggested the other ways in the previous comment to give you other options if you weren't satisfied with what you have now! – Inability Jul 17 '15 at 19:45
  • OK I got you! I could just leave the method there to be sure. I just wanted to see if anyone else have noticed this. – Michael Kornelakis Jul 17 '15 at 19:49