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 trialSteve Hunter
57,712 PointsDifficulties assigning TextField values to variables.
Here's the question in the code challenge and my proposed solution which doesn't work. I'm not sure what glaringly obvious mistake I've made!
Any thoughts?
In our view controller below we want to sign up new users for our app. In the signup method, start by capturing the username and password from two UITextField properties: 'usernameField' and 'passwordField'. Store the values in the properties 'username' and 'password'.
- (IBAction)signup:(id)sender {
NSString *username = [self.usernameField.text];
NSString *password = [self.passwordField.text];
}
4 Answers
Thomas Nilsen
14,957 PointsI think the question is badly frased, but anyway - here is the solution:
self.username = [self.usernameField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
self.password = [self.passwordField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
Thomas Nilsen
14,957 PointsFirst of all, writing it like this:
[self.usernameField.text];
is wrong. Don't do that :)
Here are two ways you can write it as
self.usernameField.text; //like this
[self.usernameField text]; //or this
Now, as for your problems: If your having problems with assigning values, I'd start by checking that your IBOutlets are properly connected. If that doesn't help, let me know :)
Thomas Nilsen
14,957 PointsAh sorry, I didn't see it was a code challenge you had problems with. I'll have a look and have an answer for you shortly :)
Steve Hunter
57,712 PointsBrilliant - thank you! :-)
Steve Hunter
57,712 PointsSteve Hunter
57,712 PointsI took the square brackets out ... same result.