views:

233

answers:

3

Hi,

I am trying to get a string value out of a textField when the user dismisses the keyboard. However, it seems that whenever I try to get the value, I get garbage (attempting to print out textField.text gives out garbage). What could I be doing wrong?

(The control displays fine, and I can put text values into it even).

Here's my code:

-(BOOL)textFieldShouldReturn:(UITextField *)textField {

    NSInteger currenttag = textField.tag;

    NSLog(@"%d",textField.tag);

    if (currenttag == 0) {
     NSLog(@"%x %s",(unsigned int)textField.text,textField.text);
     username = textField.text;
    } else if (currenttag == 1) {
     password = textField.text;
    }

    [textField resignFirstResponder];

    return YES;
}

The fields username and passwords are nil NSString*'s, but since I will merely hold on to the NSStrings held by textField.text, it should be fine.

+1  A: 
NSLog(@"text field text:%@",textField.text);
Prakash
A: 

Have you tried using breakpoints? Have you tried NSLog(@"%@", textField.text); ? Have you tried rewriting the function so it only displays the text?

Is the textField a valid object?

MiRAGe
Checked via debugger, it is indeed a valid object with the text I entered through the UI earlier.
futureelite7
In the debugger, the text you've entered should then also be readable.
MiRAGe
A: 

Inserting [textField retain]; as the 1st line will probably fix the problem. Just remember to add a [textField release]; at the end of the method.