显示键盘时禁用按钮

问题描述:

我做了一个包含UITextField和UITableView的应用程序。 我想禁用UITableView的所有功能/按钮,如果用户在UITextField中输入一些文本。因此,如果用户使用UITextField,则无法在UITableViewCell上滑动(通常会出现删除按钮)。所以用户只能在显示键盘时使用UITextFiled。显示键盘时禁用按钮

我该怎么做?

使用文本框的委托方法

- (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

试试这个在您的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

您可以禁用按钮时keyboardWillShow(和WillHide)通知被称为在NSNotificationCenter。

直接让YES在从KeyboardWillHideNO在其他

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