Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS Build a Self-Destructing Message iPhone App Using Parse.com as a Backend and Adding Users Signing Up New Users: Part 2 (PFUser)

Difficulties 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];

}

I took the square brackets out ... same result.

4 Answers

Thomas Nilsen
Thomas Nilsen
14,957 Points

I 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
Thomas Nilsen
14,957 Points

First 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
Thomas Nilsen
14,957 Points

Ah 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 :)

Brilliant - thank you! :-)