iOS-监听多个UITextField的输入事件来限制字数

textfield输入监听.gif

定制navigationBar

self.navigationBar.tintColor = [GOVCommon colorForKey:@"mainBlueColor"];
    self.navigationItem.title = @"实人认证";
    [self.navigationBar setTitleTextAttributes:@{NSForegroundColorAttributeName:[GOVCommon colorForKey:@"enNavigationTitleColor"],NSFontAttributeName : FONT_SIZE_MENUCOLUMNTITLE_EN}];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[GOVCommon loadAppImage:@"backButton"]
  1. 添加监听
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldEditing:) name:UITextFieldTextDidChangeNotification object:nil];

  1. 实现方法,获取当前监听的textField
- (void)textFieldEditing:(NSNotification *)info{
    UITextField *textField = (UITextField *)info.object;
    // 字数限制
    if (textField.tag == 10) {
        if (textField.text.length > 15) {
            textField.text = [textField.text substringToIndex:15];
        }
        self.fullNameStr = textField.text;
    }else if (textField.tag == 12){
        if (textField.text.length > 6) {
            textField.text = [textField.text substringToIndex:6];
        }
        self.zipCodeStr = textField.text;
    }else if (textField.tag == 13){
        if (textField.text.length > 11) {
            textField.text = [textField.text substringToIndex:11];
        }
        self.phoneNumberStr = textField.text;
    }
    [self saveButtonChangeColor]; 
}
// 键盘区分
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    if (textField.tag == 12 || textField.tag == 13) {
        textField.keyboardType = UIKeyboardTypePhonePad;
    }else{
        textField.keyboardType = UIKeyboardTypeDefault;
    }
}
// 必填项填后提交按钮可点
- (void)saveButtonChangeColor{
// 去除空格
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    self.fullNameStr = [self.fullNameStr stringByTrimmingCharactersInSet:set];
    self.addressStr = [self.addressStr stringByTrimmingCharactersInSet:set];
    self.zipCodeStr = [self.zipCodeStr stringByTrimmingCharactersInSet:set];
    self.phoneNumberStr = [self.phoneNumberStr stringByTrimmingCharactersInSet:set];
    
    if (![self.fullNameStr isEqualToString:@""] && ![self.addressStr isEqualToString:@""] && ![self.addressStr isEqualToString:[CDCommon localizedString:@"addressPlaceHolder"]] && ![self.phoneNumberStr isEqualToString:@""]) {
        self.saveButton.backgroundColor = [CDCommon colorForKey:@"DarkBlueThemeColor"];
        self.saveButton.enabled = YES;
    }else{
        self.saveButton.enabled = NO;
        self.saveButton.backgroundColor = [CDCommon colorForKey:@"feedbackSubmitColor"];
    }
}
  1. remove监听
- (void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

4.单个textField的输入监听也可以通过添加事件来实现

[self.textField addTarget:self action:@selector(textFieldDidChange:) forControlEvents:UIControlEventEditingChanged];
- (void)textFieldDidChange:(UITextField *)textField{
    textField.text = [textField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
    if (textField.text.length > 5) {
        textField.text = [textField.text substringToIndex:5];
    }
}

键盘的各种样式

UIKeyboardTypeDefault,                //默认键盘 Default type for the current input method.
    UIKeyboardTypeASCIICapable,           // Displays a keyboard which can enter ASCII characters
    UIKeyboardTypeNumbersAndPunctuation,  // Numbers and assorted punctuation.
    UIKeyboardTypeURL,                    // A type optimized for URL entry (shows . / .com prominently).
    UIKeyboardTypeNumberPad,              // A number pad with locale-appropriate digits (0-9, ۰-۹, ०-९, etc.). Suitable for PIN entry.
    UIKeyboardTypePhonePad,               // A phone pad (1-9, *, 0, #, with letters under the numbers).
    UIKeyboardTypeNamePhonePad,           // A type optimized for entering a person's name or phone number.
    UIKeyboardTypeEmailAddress,           // A type optimized for multiple email address entry (shows space @ . prominently).
    UIKeyboardTypeDecimalPad NS_ENUM_AVAILABLE_IOS(4_1),   // A number pad with a decimal point.
    UIKeyboardTypeTwitter NS_ENUM_AVAILABLE_IOS(5_0),      // A type optimized for twitter text entry (easy access to @ #)
    UIKeyboardTypeWebSearch NS_ENUM_AVAILABLE_IOS(7_0),    // A default keyboard type with URL-oriented addition (shows space . prominently).
    UIKeyboardTypeASCIICapableNumberPad NS_ENUM_AVAILABLE_IOS(10_0), // A number pad (0-9) that will always be ASCII digits.

你可能感兴趣的:(iOS-监听多个UITextField的输入事件来限制字数)