-1

I put a function in my code like this:

func textFieldShouldReturn(_ textField: UITextField) -> Bool { textfield.resignFirstResponder() return true } 

where textfield is connect to a UITextField item.

@IBOutlet weak var textfield: UITextField! 

but when I run the program, actually I cannot hide it wherever I tap or click. Could you please help me to solve it?

2
  • Have you connected textfield's delegate? Commented May 9, 2017 at 17:14
  • What is that??? I don't know actually..... Commented May 9, 2017 at 17:19

2 Answers 2

0

Need to set delegate

override func viewDidLoad() { super.viewDidLoad() textfield.delegate = self //set delegate } 
Sign up to request clarification or add additional context in comments.

3 Comments

Since there is an outlet, why do this in code? Connect the text field's delegate in the storyboard.
I added delegate setting. now I can dismiss keyboard by clicking on return button. if I want to dismiss it when I tap at anywhere on the screen. how and why ? thanks?
try this : override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { self.view.endEditing(true) }
-2

add this delegate UITextFieldDelegate

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.