title: iOS Development Knowledge Point 2 - Login Interface
date: 2015-11-17 21:49
tags: Technology#
iOS Development Knowledge Point 2 - Login Interface#
Real-time response of buttons: When logging in, the button should only respond (be clickable) when the user has entered something. If any of the input fields are empty, the button should not respond. I used to think that using the delegate method of textfield to directly judge was the way to go, but later I found that it was not good. There is another method.
// First, add addTarget method to textfield, note that the last controlEvents is EditingChanged
[self.accountTextField addTarget:self action:@selector(handleTextDidChange:) forControlEvents:UIControlEventEditingChanged];
[self.passwordTextField addTarget:self action:@selector(handleTextDidChange:) forControlEvents:UIControlEventEditingChanged];
// Then implement this method
- (void)handleTextDidChange:(id)sender
{
NSString * accountStr = self.accountTextField.text;
NSString * passwordStr = self.passwordTextField.text;
if (accountStr.length > 0 && passwordStr.length > 0) {
// Login can respond
self.loginButton.userInteractionEnabled = YES;
[self.loginButton setTitleColor:btnEnabledColor forState:UIControlStateNormal];
} else {
// Login cannot respond
self.loginButton.userInteractionEnabled = NO;
[self.loginButton setTitleColor:btnUnabledColor forState:UIControlStateNormal];
}
}
Input field length limitation: When entering a phone number or verification code, there is usually a length limitation. What we want is an 11-digit phone number or a 6-digit verification code, so we can only input up to this length. When it exceeds, it should not be displayed anymore for user understanding.
// This is implemented through the delegate method of textField
_smsPhoneTextField.delegate = self;
_smsCirtifyTextField.delegate = self;
#pragma mark - Input Length Limitation
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ([string isEqual:@"\n"]) {
return YES;
}
NSString * aString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (textField == _smsCirtifyTextField) {
if (aString.length > 6) {
aString = [aString substringToIndex:6];
return NO;
}
}
if (textField == _smsPhoneTextField) {
if (aString.length > 11) {
aString = [aString substringToIndex:11];
return NO;
}
}
return YES;
}
Regular expression for phone number: I have bookmarked a blog about commonly used regular expressions, it's very good. You just need to modify it based on your own needs to use it.
Commonly Used Regular Expressions
#pragma mark - Mobile Number Validation
- (BOOL)validateMobile:(NSString *)mobile
{
// Mobile number starts with 13, 15, 18, or 17 followed by eight \d numeric characters
NSString * phoneRegex = @"^((13[0-9])|(15[^4,\\D])|(18[0,0-9])|(17[0,0-9]))\\d{8}$";
NSPredicate * phoneTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", phoneRegex];
return [phoneTest evaluateWithObject:mobile];
}