iOS开发Textfield输入纯数字

来源:互联网 发布:java反编译 编辑:程序博客网 时间:2024/06/05 04:04

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

{


    NSCharacterSet*cs;


    cs = [[NSCharacterSetcharacterSetWithCharactersInString:@"0123456789"]invertedSet];

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

    BOOL basicTest = [stringisEqualToString:filtered];

    if(!basicTest) {

        

        UIAlertController *alert=[UIAlertControlleralertControllerWithTitle:@"提示"message:@"请输入数字"preferredStyle:UIAlertControllerStyleAlert];

        [alert addAction:[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

            

            

        }]];

        [selfpresentViewController:alertanimated:YEScompletion:nil];


        returnNO;

        

    }

    if (textField.text.length==6) {

        UIAlertController *alert=[UIAlertControlleralertControllerWithTitle:@"提示"message:@"密码为6位数字"preferredStyle:UIAlertControllerStyleAlert];

        [alert addAction:[UIAlertActionactionWithTitle:@"确定"style:UIAlertActionStyleDefaulthandler:^(UIAlertAction *_Nonnull action) {

            

            

        }]];

        [selfpresentViewController:alertanimated:YEScompletion:nil];

        returnNO;

    }

    returnYES;

}

0 0
原创粉丝点击