我有一個UITextfield
中,我想用戶輸入逗號分隔的文本限制用戶輸入只有七個逗號分隔的單詞
例如:象,狐狸,等,此外,我想限制用戶以最大7進入逗號分隔的單詞。
我已經在UITextfield
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSArray *numberOfWords = [self.tagsTextField.text componentsSeparatedByString:@","];
if([numberOfWords count] > 6)
{
return NO;
}
else
{
return YES;
}
}
委託方法我無法達到預期的行爲,請幫忙寫這段代碼!
也許你想使用一個簡單的正則表達式的呢? 這可能有助於:[鏈接](https://www.raywenderlich.com/30288/nsregularexpression-tutorial-and-cheat-sheet) – ConquerorsHaki
您嘗試以未改變的文本獲取字數。您需要應用更改,然後嘗試獲得單詞數量。 ** [self.tagsTextField.text stringByReplacingCharactersInRange:range withString:string] **。但我認爲這是糟糕的用戶體驗。我作爲用戶不喜歡當我不能申請一些改變和應用程序不告訴我什麼錯誤。允許輸入更多的世界,但顯示錯誤。 – Sergey
上述代碼中的問題是什麼? – Birendra