【问题标题】:tap on UITextField not working点击 UITextField 不起作用
【发布时间】:2017-02-07 09:03:33
【问题描述】:

我读了很多书试图找到一种方法来处理 uitextfield 点击,但对我没有任何作用

我想要什么:

我想在 UITextField 上添加点击并打开对话框

我尝试了什么:

class CreateMessagesViewController: UIViewController {

    @IBOutlet weak var testField: UITextField!
    @IBOutlet weak var testView: UIView!

    override func viewDidLoad() {
        super.viewDidLoad()

        let tap = UITapGestureRecognizer(target: self, action: #selector(self.handleTap(_:)))
//        testField.addGestureRecognizer(tap)
        testView.addGestureRecognizer(tap)

        testField.addTarget(self, action: #selector(onTapped(_:)), for: .touchDown)
    }

    func onTapped(_ sender : UITextField){

        print("Hello World")
    }

    func handleTap(_ sender: UITapGestureRecognizer) {
        print("Hello World")
    }
}

如您所见,我尝试了两种方法:

  1. UITapGestureRecognizer
  2. 选择器

为了测试,我添加了简单的 UIView 以确保点击正常工作。

为什么它在 uiTextField 上不起作用?我有什么遗漏的吗?

【问题讨论】:

标签: ios objective-c iphone swift swift3


【解决方案1】:

您可以编写打开对话框的代码,而不是添加手势识别器

func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
    openDialog()//Function which will open the dialog
    return false
}

我觉得应该和你想要的效果一样。

【讨论】:

    【解决方案2】:

    1)添加手势识别器:

    let tapGR = UITapGestureRecognizer(target: self, action: #selector(someFunc))
    tapGR.delegate = self
    textField.addGestureRecognizer(tapGR)
    

    2)确保在必要时允许您的点击处理程序:

    func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {
        return true //or add your custom check here if you need
    }
    

    如果没有第二部分,你会得到一个错误的 textField,其中有时点击手势不起作用,有时默认手势未处理!

    【讨论】:

      【解决方案3】:

      将 GestureRecognizer 添加到 UITextField superview 并检查是否有帮助:

      testField.superview.addGestureRecognizer(tap)
      

      【讨论】:

      • superview 不是这里的解决方案。
      【解决方案4】:

      我为您的问题创建了 Swift 和 Objective C 的示例之一,但它不起作用。我使用了 textField 的 Tap Gesture 识别器代码,但它甚至没有被调用。

      在 Swift 和 Objective C 中的代码下面不工作

      斯威夫特

      let tap = UITapGestureRecognizer(target: self,action: #selector(handleTaponTextField(_:)))
      tap.numberOfTapsRequired = 1
      tap.delegate = self
      txtFldTapMe.isUserInteractionEnabled = true
      txtFldTapMe.addGestureRecognizer(tap)
      
      func handleTaponTextField(_ sender: UITapGestureRecognizer)
      {
          print("Tap is handled here!")
      }
      

      目标 C

      UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];
      tapGesture.numberOfTapsRequired = 1;
      tapGesture.delegate = self;
      txtfldTapME.userInteractionEnabled = YES;
      [txtfldTapME addGestureRecognizer:tapGesture];
      
      func handleTaponTextField(_ sender: UITapGestureRecognizer)
      {
          print("Tap is handled here!")
      }
      

      然后我尝试使用以下 addTarget 操作方法为 textField 并且它工作正常。

      在 Swift 和 Objective C 中的代码下方工作

      斯威夫特

      txtFldTapMe.addTarget(self, action: #selector(textFieldEditDidBegin(_:)), for: .editingDidBegin)
      
      func textFieldEditDidBegin(_ textField: UITextField) {
          print("Text editing begin here!")
      }
      

      查看方法

      你应该使用上面的textField方法

      目标 C

      [txtfldTapME addTarget:self action:@selector(editingDidBeginStarted:) forControlEvents: UIControlEventEditingDidBegin];   
      
      -(void)editingDidBeginStarted:(UITapGestureRecognizer *)sender{
        NSLog(@"Editing started here");
      }
      

      请参阅下面的 textField 方法

      【讨论】:

      • 您的tap.delegate = self 是做什么的? UITapGestureRecognizer 似乎因为您遗漏了某些东西而无法正常工作。
      【解决方案5】:

      有一种更简单的方法可以实现这一点。您应该确认您的视图控制器类为UITextFieldDelegate 并在您的类中实现textFieldDidBeginEditing

      func textFieldDidBeginEditing(_ textField: UITextField){
             //show popup here
      }
      

      如果您有多个文本字段,请在情节提要(或代码)中为您的文本字段分配一个标签以识别文本字段。

       func textFieldDidBeginEditing(_ textField: UITextField){
      
      // check your tag here
         if textField.tag == 0{
      
                  //show your popup here
              }
      
      }
      

      【讨论】:

        【解决方案6】:

        试试下面的代码

        yourTextField.addTarget(self, action: #selector(didChangeText), for: .editingChanged)
            addSubview(view)
        
        @objc func didChangeText(textField:UITextField) {
            let str = textField.text
                //your functionality here
        }
        

        【讨论】:

          【解决方案7】:

          这是不可能的。因为Textfield委托方法调用。

          【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-12-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多