美文网首页
限制UItextField只能输入小数点和数字

限制UItextField只能输入小数点和数字

作者: 南巷旧人M | 来源:发表于2016-08-17 16:17 被阅读237次

    作者:孟令文

    #define NUMBERS @"0123456789."

    - (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string

    {

    if(textField ==self.textField05){

    NSCharacterSet*cs;

    cs = [[NSCharacterSetcharacterSetWithCharactersInString:NUMBERS]invertedSet];

    NSString*filtered = [[stringcomponentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];

    BOOLbasicTest = [stringisEqualToString:filtered];

    if(!basicTest) {

    UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:@"提示"

    message:@"请输入数字"

    delegate:nil

    cancelButtonTitle:@"确定"

    otherButtonTitles:nil];

    [alertshow];

    returnNO;

    }

    returnYES;

    }

    if(textField ==self.textField06){

    NSCharacterSet*cs;

    cs = [[NSCharacterSetcharacterSetWithCharactersInString:NUMBERS]invertedSet];

    NSString*filtered = [[stringcomponentsSeparatedByCharactersInSet:cs]componentsJoinedByString:@""];

    BOOLbasicTest = [stringisEqualToString:filtered];

    if(!basicTest) {

    UIAlertView* alert = [[UIAlertViewalloc]initWithTitle:@"提示"

    message:@"请输入数字"

    delegate:nil

    cancelButtonTitle:@"确定"

    otherButtonTitles:nil];

    [alertshow];

    returnNO;

    }

    returnYES;

    }

    returnYES;

    }

    相关文章

      网友评论

          本文标题:限制UItextField只能输入小数点和数字

          本文链接:https://www.haomeiwen.com/subject/iybmsttx.html