iOS 限制只能输入数字(字母)

#define NUMBERS @"0123456789"

#define ENGLISH @"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefjhigklmnopqrstuvwxyz"

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

{

    NSCharacterSet*cs;

    cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS] invertedSet];

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

    BOOL basicTest = [string isEqualToString:filtered];

    if(!basicTest) {

        

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

                                                       message:@"请输入数字"

                                                      delegate:nil

                                             cancelButtonTitle:@"确定"

                                             otherButtonTitles:nil];

        

        [alert show];

        return NO;

        

    }

    return YES;

}

posted @ 2016-07-22 13:58  瀚海枭龙  阅读(1660)  评论(0编辑  收藏  举报