2015-04-02 199 views
0

我做了一個包含UITextField和UITableView的應用程序。 我想禁用UITableView的所有功能/按鈕,如果用戶在UITextField中輸入一些文本。因此,如果用戶使用UITextField,則無法在UITableViewCell上滑動(通常會出現刪除按鈕)。所以用戶只能在顯示鍵盤時使用UITextFiled。顯示鍵盤時禁用按鈕

我該怎麼做?

回答

4

使用文本框的委託方法

- (void)textFieldDidBeginEditing:(UITextField *)textField{ 
     //check if(textfield==yourtextfield) if you have more than one textfields 
      tableView.userInteractionEnabled = NO; 
} 


- (void)textFieldDidEndEditing:(UITextField *)textField{ 
    tableView.userInteractionEnabled = YES; 
} 
+0

感謝您的回答!有用。 – user3592462 2015-04-02 19:37:27

0

試試這個在您的UITextField的委託方法:

- (BOOL) textField: (UITextField *)theTextField shouldChangeCharactersInRange: (NSRange)range replacementString: (NSString *)string 
{  
if (theTextField == yourTextField){  
    yourButtonOutlet.userInteractionEnabled = NO;  
} 
} 
+0

這個答案是唯一有效的,當用戶鍵入..按鈕應爲文本框被禁用稱爲選擇成爲第一個響應者,如回答以上建議。 – rcat24 2015-04-02 15:35:30

1

您可以禁用按鈕時keyboardWillShow(和WillHide)通知被稱爲在NSNotificationCenter。

直接讓YES在從KeyboardWillHideNO在其他

- (void)keyboardWillShow:(NSNotification *)notification { 
    //disable button here 
}