Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Reuben Varzea
23,182 PointsiOS: Hiding the keyboard programmatically in Swift
I would like the keyboard to be removed when a user presses "Return" on the soft keyboard. I've read through the documentation for doing this (both in Objective-C and now in Swift), but I'm apparently missing something.
I've added these lines to my ViewController:
// Added UITextFieldDelegate to the class declaration
class ViewController: UIViewController, UITextFieldDelegate
// I've connected the textfield
@IBOutlet weak var userText: UITextField!
// Added the following function
func textFieldShouldReturn(userText: UITextField!) -> Bool {
userText.resignFirstResponder()
return true;
}
I'm sure I'm missing something, but I'm just not seeing it. Help?
2 Answers

Stone Preston
42,016 Pointsyou need to be sure and set your view controller as the text fields delegate in view did load:
override func viewDidLoad() {
super.viewDidLoad()
self.userText.delegate = self;
}

Tal Zion
6,234 PointsHi guys, I may bee far ahead but how do we dismiss the keyboard on a UITextView?
Thanks
Reuben Varzea
23,182 PointsReuben Varzea
23,182 PointsTHAT did it. Thanks Stone Preston !!
Stone Preston
42,016 PointsStone Preston
42,016 Pointsno problem : )