0

我想创建一个登录但制作一个 IF 语句,它将提供 UIButton -“LoginButton”功能,如果输入正确,如代码中所述,请帮助构建代码 -

#import "StudentLogInViewController.h"

@interface StudentLogInViewController ()

@end

@implementation StudentLogInViewController

-(IBAction)UsernametText {
LoginButton.userInteractionEnabled = [UsernameText.text isEqualToString:@"jzarate"];
}

-(IBAction)passwordText{
    LoginButton.userInteractionEnabled = [PasswordText.text isEqualToString:@"14054"];
}
4

1 回答 1

0

如果我没有提到只有在输入正确的用户名和密码时才启用登录按钮,从安全的角度来看,这可能不是最好的主意,那我就失职了。但是,如果这是您想要做的:

// 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
于 2013-03-27T08:45:54.890 回答