【问题标题】:Disable buttons while keyboard is displayed显示键盘时禁用按钮
【发布时间】:2015-06-07 15:08:30
【问题描述】:

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

我该怎么做?

【问题讨论】:

    标签: ios swift xcode6


    【解决方案1】:

    使用文本字段委托方法

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

    【讨论】:

      【解决方案2】:

      您可以在 NSNotificationCenter 中调用 keyboardWillShow(和 WillHide)通知时禁用按钮。

      只需在从KeyboardWillHideNO 调用的选择器中调用YES

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

      【讨论】:

        【解决方案3】:

        在你的 UITextField 的委托方法中试试这个:

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

        【讨论】:

        • 此答案仅在用户输入时有效。应禁用按钮,因为 textField 成为第一响应者,如上面的答案所示。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-08-25
        • 1970-01-01
        • 2021-08-11
        • 2010-12-31
        • 1970-01-01
        相关资源
        最近更新 更多