本字段上的UITapGestureRecognizer与预期不符

本字段上的UITapGestureRecognizer与预期不符

本文介绍了文本字段上的UITapGestureRecognizer与预期不符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的课程中,我在一个数组textViewRecognizer中有11个UITapGestureRecognizer,附加到数组boxArray中的100个UITextField中的11个。当点击包含UIGestureRecognizer的Textfield时,它会运行tappedTextView,我尝试获取第一个响应者的索引。

In my class I have 11 UITapGestureRecognizers in an array textViewRecognizer attached to 11 out of 100 UITextFields in an array boxArray. When a Textfield is tapped containing a UIGestureRecognizer it runs tappedTextView where I try to get the index of the first responder.

然而,由于对事物执行方式的一些奇怪的排序,动作函数只给我前一个第一响应者的第一个响应者到刚被点击的响应者。

However, due to some weird ordering in how things are executed, the action function only gives me the first responder of the previous first responder to the one that was just tapped.

另外,我必须双击甚至选择我想要的文本字段!我需要使用点击功能,而不是文本代表,所以这真的很头疼。

Also, I have to double tap to even select the text field I was going for! I need to use the tap function and not the text delegates so this has been a real headache.

我有......

    @objc func tappedTextField(_ sender: UITapGestureRecognizer) {
    for i in 0...99 {
        if (boxArray[i]?.isFirstResponder)! {
            if let index = boxArray.index(of: boxArray[i]) {
            print(index)
            break
            }
        }
    }
}

在我的viewDidLoad中我有

in my viewDidLoad I have

for i in 0...10 {
        textFieldTapRecognizer[i].addTarget(self, action: #selector(self.tappedTextField(_:)))
}

在我班上我有

我想在100个textFields中设置11个以根据某些条件设置一个tap识别器(我将在这里使用常规for循环)

I want to set 11 out of 100 textFields to have this a tap recognizer depending on some conditions (I'm just going to use a regular for loop here)

for i in 0...10 {
         boxArray[i]?.addGestureRecognizer(textFieldTapRecognizer[i])
}




  1. 无论如何,在点击完成后我能得到它给我实际的第一响应者吗?

  1. Is there anyway I can get it to give me the actual first responder, after the tap was made?

是否有双击选择具有UITapGesture的文本字段?

Is there anyway to go around the double tap to select the text field that has a UITapGesture?

非常感谢任何帮助。

编辑:正确命名的函数

推荐答案

我会在我的 UITextView 中添加一个标签 UITextViewDelegate 设置为我的 ViewController

I would add a Tag to my UITextView and set the UITextViewDelegate to my ViewController.

然后我将添加以下Delegate方法:

Then I would add the following Delegate method:

func textViewShouldBeginEditing(_ textView: UITextView) -> Bool {
    print("Textview tag: ", textView.tag)
    return false
}

这篇关于文本字段上的UITapGestureRecognizer与预期不符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 09:19