我正在嘗試創建一個問卷,其中用戶應對1-7中的語句進行評分。文本驗證 - 用戶只能選擇一個數字一次
用戶通過填入UITextFields
的值爲1-7之間的值對語句進行評級。
我已經做到這樣,用戶只能寫入1-7之間的值,並且他們只能爲每個文本字段寫入一個字符。
我現在想要做的是防止用戶多次使用該值。
這是我到目前爲止的代碼。
#define CHARACTERS @"1234567"
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
// These are the characters that are ~not~ acceptable
NSCharacterSet *unacceptedInput = [[NSCharacterSet characterSetWithCharactersInString:CHARACTERS] invertedSet];
NSString *newString = [textField.text stringByReplacingCharactersInRange:range withString:string];
if (([[string componentsSeparatedByCharactersInSet:unacceptedInput] count] >1) | ([newString length] > 1))
return NO;
else
return YES;
}
你的意思是,如果用戶通過輸入文本「1」來評價,那麼另一個文本框不應該包含「1」,對嗎? – dks1725 2011-04-20 13:02:06
是的,你是對的。 – Makkafella 2011-04-20 13:04:45
@ dks1725 [tf1.text isEqualToString:string]解決了它! (tf1是其中一個textfield的出口) – Makkafella 2011-04-20 14:44:03