我有一點問題。 我的TableView中有兩個textField,用於設置對象的屬性。爲了做到這一點,我希望強制用戶在字符串被設置爲對象之前在textField中寫入一些內容。所以基本上是一個簡單的([textField.text length] > 0)
的事情。 但我希望用戶必須在兩個textField中編寫字符串以最終啓用「完成」 - 按鈕。 我早些時候解決了這個問題,但只有一個文本字段與以下UITextFieldDelegate
方法。UITextFieldDelegate,檢查文本字段是否有文本
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
self.doneBarButton.enabled = ([newText length] > 0);
return YES;
}
我對新的問題的解決方案,所以現在有兩個文本框是這個:
- (BOOL)textField:(UITextField *)theTextField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *newText = [theTextField.text stringByReplacingCharactersInRange:range withString:string];
if ([theTextField.placeholder isEqualToString:@"textField1"]) {
if ([theTextField.text length] > 0) {
enabledVokabel = YES;
} else {
enabledVokabel = NO;
}
}
if ([theTextField.placeholder isEqualToString:@"textField2"]) {
if ([theTextField.text length] > 0) {
enabledUebersetung = YES;
} else {
enabledUebersetung = NO;
}
}
self.doneBarButton.enabled = (enabledVokabel && enabledUebersetung);
return YES;
}
所以我想在這兩個文本框(textField1的和文字欄)都充滿doneBarButton啓用文本。但是我想這樣,如果用戶刪除了文本,他/她剛纔在doneBarButton中寫入的文本在textFields爲空時被禁用。 它不這樣工作。你有解決方案嗎?或者更好的方法來解決它?
[UITextField text change event] [1] 希望這個幫助一下! [1]:http://stackoverflow.com/questions/7010547/uitextfield-text-change-event –