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

iOS

How to clear textfield and hide keyboard once done typing? IN SWIFT

On the second view controller, Once I press "add" button I would like the keyboard to automatically go down and the text field to clear whatever text is in there. Please Help!

https://www.dropbox.com/s/wmg4jc7ybwie93u/guessWho.zip?dl=0

1 Answer

Hide the keyboard "automatically", click here for a helpful Stack Overflow article.

To clear text fields programmatically, click here for another Stack Overflow article

These will both give you a really good head start, but may take a little more digging to get the details.

Good luck! -- Kyle

Everything you gave me is in Objective-c. I need swift.

I can see why that would be difficult. However, it would be extremely wise to gain some experience in Objective C, as much of the existing code for iOS is in ObjC, thus many of the frameworks and libraries you'll be using will be in ObjC. It was difficult to go from one to the other, at first, for me as well. Here's a simple set of examples that compare both languages.

As for the links I gave you, a good way to translate what the users are saying from ObjC to Swift is as follows (I'll take the first link on hiding the keyboard):

[self.view.window endEditing:YES];

is the same thing is Swift as:

self.view.window?.endEditing(true)

Those brackets "[" and "]" in ObjC mean everything inside is a method call. A good approach to finding out if something works in Swift is to start typing the method name, which be very similar to the ObjC method. In this case, self.view.window was the object, then you call endEditing(true) on that object and it invokes the same method you find in ObjC. Xcode is good at helping you fill in the gaps, like the "?" you'll need after self.view.window.

I'm sorry for the initial confusion, but as I said, knowing ObjC is an incredibly helpful skill to have. I'd recommend spending some time in the earlier ObjC courses on Treehouse, here. They will help you dig into ObjC. That's pretty much how I got started with ObjC myself.

Good luck! -- Kyle