-2

I'm having an annoying problem where my UITextField will return the incorrect value. Typing "test" in the UITextField called "username" should create an alert view titled "Good". But it is showing my error alert view instead.

Does anyone have any idea what I've done wrong?

Thanks

- (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];
    }
}
4

1 回答 1

4

使用isEqualToString方法而不是'=='

if ([givenUsername isEqualToString:properUsername])
于 2013-06-10T04:21:17.363 回答