正しいユーザー名とパスワードが入力されたときにのみログインボタンを有効にすることは、セキュリティの観点からおそらく最善のアイデアではないことを述べなかったとしたら、私は失望するでしょう。ただし、それがあなたのやりたいことである場合:
// 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