1.在.h繼承<UITextFieldDelegate>
2.在.m將設定self.stockNumberField.delegate = self;
3.實作以下metohd
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    // allow backspace
    if (!string.length){
        return YES;
    }
   
    // Prevent invalid character input, if keyboard is numberpad
    if (textField.keyboardType == UIKeyboardTypeNumberPad){
        if ([string rangeOfCharacterFromSet:[[NSCharacterSet decimalDigitCharacterSet] invertedSet]].location != NSNotFound){
            // BasicAlert(@"", @"This field accepts only numeric entries.");
            return NO;
        }
    }
   
    // verify max length has not been exceeded
    NSString *updatedText = [textField.text stringByReplacingCharactersInRange:range withString:string];
   
    if (updatedText.length > 4){ // 4 was chosen for SSN verification
        // suppress the max length message only when the user is typing
        // easy: pasted data has a length greater than 1; who copy/pastes one character?
        if (string.length > 1){
            // BasicAlert(@"", @"This field accepts a maximum of 4 characters.");
        }
       
        return NO;
    }
   
    // only enable the OK/submit button if they have entered all numbers for the last four of their SSN (prevents early submissions/trips to authentication server)
    //self.answerButton.enabled = (updatedText.length == 4);
   
    return YES;
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Kirk 的頭像
    Kirk

    生活+工作+攝影

    Kirk 發表在 痞客邦 留言(0) 人氣()