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 trialalex santorineos
4,345 PointsHow 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!
1 Answer
Kyle Pontius
6,190 PointsHide 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
alex santorineos
4,345 Pointsalex santorineos
4,345 PointsEverything you gave me is in Objective-c. I need swift.
Kyle Pontius
6,190 PointsKyle Pontius
6,190 PointsI 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 callendEditing(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 afterself.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