2011-08-05 10 views
0

テキストフィールドに番号を書く必要があります。この番号はポイント(10.23)またはポインティングなし(10)です。しかし、私はこの数字が単語ではなく数字であることを確認する必要があります。テキストフィールドに単語の代わりに数字があるかどうか確認できますか? 例:数字の代わりに単語を書くと、エラーがあると言うnslogが必要です。IOS:テキストフィールドのテキストを確認してください

答えて

3

別の方法:

NSString *string = @"684.14"; 
NSCharacterSet *decimalSet = [NSCharacterSet decimalDigitCharacterSet]; 
BOOL stringIsValid = ([[string stringByTrimmingCharactersInSet:decimalSet] isEqualToString:@""] || 
         [[string stringByTrimmingCharactersInSet:decimalSet] isEqualToString:@"."]); 

はまた、あなたがキーボードのためtextField.keyboardType = UIKeyboardTypeNumberPad;を使用できることを覚えておいてください。 (これは数値入力のみを保証するものではありませんが、ユーザーフレンドリーなのです)

0

チェックアウトNSScannerhttps://developer.apple.com/library/ios/#documentation/Cocoa/Reference/Foundation/Classes/NSScanner_Class/Reference/Reference.html#//apple_ref/doc/uid/TP40003726

if([[NSScanner scannerWithString:@"-123.4e5"] scanFloat:NULL]) 
    NSLog(@"\"-123.4e5\" is numeric"); 
else 
    NSLog(@"\"-123.4e5\" is not numeric"); 
if([[NSScanner scannerWithString:@"Not a number"] scanFloat:NULL]) 
    NSLog(@"\"Not a number\" is numeric"); 
else 
    NSLog(@"\"Not a number\" is not numeric"); 
// prints: "-123.4e5" is numeric 
// prints: "Not a number" is not numeric 
関連する問題