UITextField返回不正确的值

时间:2013-06-10 04:18:17

标签: ios objective-c uitextfield uialertview

我遇到了一个恼人的问题,我的UITextField会返回错误的值。在名为“username”的UITextField中键入“test”应创建一个标题为“Good”的警报视图。但它显示我的错误警报视图。

有谁知道我做错了什么?

由于

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (IBAction)buttonPress {
    NSString *properUsername = @"test";
    NSString *givenUsername = self.username.text;
    if(givenUsername == properUsername){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Good"
                                                        message:nil
                                                       delegate:nil
                                              cancelButtonTitle:@"Okay"
                                              otherButtonTitles:nil];
        [alert show];
    }
    else {

        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Wrong username/password combination"
                                                        message:nil
                                                       delegate:nil
                                              cancelButtonTitle:@"Okay"
                                              otherButtonTitles:nil];

        [alert show];
    }
}

1 个答案:

答案 0 :(得分:4)

使用isEqualToString方法代替'=='

if ([givenUsername isEqualToString:properUsername])