开发者

UITextField text value returns garbage

开发者 https://www.devze.com 2022-12-13 10:15 出处:网络
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

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 an开发者_运维百科d passwords are nil NSString*'s, but since I will merely hold on to the NSStrings held by textField.text, it should be fine.


NSLog(@"text field text:%@",textField.text);


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号