不能在UITextField上轻按

问题描述 投票:2回答:7

我读了很多,试图找到一种处理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
7个回答
3
投票
代替添加手势识别器,您可以编写用于打开对话框的代码

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

我认为它应该具有您想要的效果。

0
投票
将GestureRecognizer添加到UITextField超级视图并检查是否有帮助:

testField.superview.addGestureRecognizer(tap)


0
投票
我为您的问题Swift和Objective C创建了示例一,但它不起作用。我对文本字段使用了Tap Gesture识别器代码,但甚至没有调用。

在Swift和Objective C中的代码下无法使用

Swift

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!") }
然后,我尝试使用下面的addField动作来处理textField,它工作正常。

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

Swift

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


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,其中有时轻按手势无效,有时无法处理默认手势!

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


0
投票
尝试下面的代码

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


-1
投票
不可能。因为Textfield委托方法调用。
© www.soinside.com 2019 - 2024. All rights reserved.