如果我没有提到只有在输入正确的用户名和密码时才启用登录按钮,从安全的角度来看,这可能不是最好的主意,那我就失职了。但是,如果这是您想要做的:
// StudentLogInViewController.h
// Conform the UITextFieldDelegate
@interface StudentLogInViewController <UITextFieldDelegate>
@end
// StudentLogInViewController.m
#import "StudentLogInViewController.h"
@interface StudentLogInViewController ()
@end
@implementation StudentLogInViewController
-(void)viewDidLoad {
[super viewDidLoad];
UsernameText.delegate = self;
PasswordText.delegate = self;
}
// This is a UITextFieldDelegate method
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
// The text field will not be updated to the newest text yet, but we know what the user just did so get it into a string
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
// Depending on which field the user is typing in, load in the appropriate inputs
NSString *username, *password;
if (textField == UsernameField) {
username = newString;
password = PasswordField.text;
} else {
username = UsernameField.text;
password = newString;
}
// If both the username and password are correct then enable the button
LoginButton.enabled = ([username isEqualToString:@"correctUsername"] && [password isEqualToString:@"correctPassword"]);
// Return YES so that the user's edits are used
return YES;
}
@end