开发者

If statement to check username and password entered in an iOS app

I need help for an if-else statement to check if the username and password matches. There'll be two text fields where the user inputs the username and password, a Submit button to login the user and also a label below it to display an error message if the username and password isn't valid. Once username and password is valid, the modal view will dismiss and take开发者_JAVA技巧 the user to the main page of the app. Below is the valid username and password:

Username: user1

Password: test123

My existing code below doesn't work.

if (usernameTextField.text == @"user1" && passwordTextField.text == @"test123")
{
    statusLabel.text = @"Success!";
    [self dismissModalViewControllerAnimated:YES];
}
else
{
    statusLabel.text = @"Incorrect. Please try again";
}

Any help would be appreciated. Thanks! :)


I dont think there's anything wrong in your code. However, try comparing the string values with the isEqualToString method and not the ==.

if([usernameTextField.text isEqualToString:@"user1"] && [passwordTextField.text isEqualToString:@"test123"])
    NSLog(@"Success");

Also, check that the textfields are connected in the nib.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜