点击UITextField无法正常工作

时间:2017-02-07 09:03:34

标签: ios objective-c iphone swift swift3

我读了很多试图找到一种处理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. 选择
  3. 为了测试,我添加简单的UIView以确保点击工作正常并且有效。

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

7 个答案:

答案 0 :(得分:3)

您可以在

中编写用于打开对话框的代码,而不是添加手势识别器
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
    openDialog()//Function which will open the dialog
    return false
}

我认为它应该具有你想要的相同效果。

答案 1 :(得分:0)

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

testField.superview.addGestureRecognizer(tap)

答案 2 :(得分:0)

我为你的问题创建了样本一,包括Swift和Objective C,但它没有用。我使用了TapFsture识别器代码用于textField,但它甚至没有调用。

在Swift和Objective C中的Code下面没有工作

<强>夫特

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!")
}

然后我尝试使用下面的textField的addTarget动作方法,它工作正常。

在Swift和Objective C中的Code下面工作

<强>夫特

txtFldTapMe.addTarget(self, action: #selector(textFieldEditDidBegin(_:)), for: .editingDidBegin)

func textFieldEditDidBegin(_ textField: UITextField) {
    print("Text editing begin here!")
}

参见方法

enter image description here

您应该使用textField的上述方法

目标C

[txtfldTapME addTarget:self action:@selector(editingDidBeginStarted:) forControlEvents: UIControlEventEditingDidBegin];   

-(void)editingDidBeginStarted:(UITapGestureRecognizer *)sender{
  NSLog(@"Editing started here");
}

参见下面的textField方法

enter image description here

答案 3 :(得分:0)

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的越野车,在该越野车中,有时轻按手势无效,有时无法处理默认手势!

答案 4 :(得分:0)

有一种更简单的方法可以实现此目的。您应该确认您的视图控制器类为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
        }

}

答案 5 :(得分:0)

尝试下面的代码

yourTextField.addTarget(self, action: #selector(didChangeText), for: .editingChanged)
    addSubview(view)

@objc func didChangeText(textField:UITextField) {
    let str = textField.text
        //your functionality here
}

答案 6 :(得分:-1)

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

相关问题