UITextField在单击时没有响应吗?

时间:2019-07-01 10:22:40

标签: ios swift uitextfield

我有一个UIImageview,它涵盖了主VC的整个视图,然后在imageView上添加了一个UIView,然后在视图中添加了两个UITextField,如下图所示。

enter image description here

现在

  • 我已将ImageView,innerView和textField的isUserInteractionEnable都标记为true
  • 我也有self.view.bringSubviewToFront(phoneNumberTextField)

但是当我尝试单击TextField时,它仍然没有响应,

override func viewDidLoad() {
    super.viewDidLoad()
    self.view.bringSubviewToFront(phoneNumberTextFieldOutlet)
    self.phoneNumberTextFieldOutlet.becomeFirstResponder()
    // Do any additional setup after loading the view.
}


@objc func textFieldDidChange(textField: UITextField){
    let text = textField.text
    if text?.utf16.count ?? 0 == 10{
        self.mobileNumber = text ?? "0"
        passwordTextFieldOutlet.becomeFirstResponder()
    }else{
        //Do nothing
    }
}

@IBOutlet weak var passwordTextFieldOutlet: UITextField!{
    didSet{
        passwordTextFieldOutlet.onlyBottomBorder()
    }
}
@IBOutlet weak var phoneNumberTextFieldOutlet: UITextField!{
    didSet{
        phoneNumberTextFieldOutlet.onlyBottomBorder()
        phoneNumberTextFieldOutlet.delegate = self
        phoneNumberTextFieldOutlet.addTarget(self, action: #selector(textFieldDidChange), for: UIControl.Event.editingChanged)
        phoneNumberTextFieldOutlet.addTarget(self, action: #selector(phoneNumberFieldTouched), for: .touchDown)
    }
}




func resetAllFields(){
    self.phoneNumberTextFieldOutlet.text = nil
    self.passwordTextFieldOutlet.text = nil
    self.phoneNumberTextFieldOutlet.becomeFirstResponder()
}

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    if textField.tag == 1{
        let aSet = NSCharacterSet(charactersIn:"0123456789").inverted
        let compSepByCharInSet = string.components(separatedBy: aSet)
        let numberFiltered = compSepByCharInSet.joined(separator: "")
        return string == numberFiltered
    }
    return true
}

@objc func phoneNumberFieldTouched(){
    self.phoneNumberTextFieldOutlet.becomeFirstResponder()
}

在调试之后,我得到了这个, Views Hierarchy

下面是情节提要的短屏幕, StoryBoard screen short

1 个答案:

答案 0 :(得分:0)

在您的代码中看不到UITextField的委派。我假设您应该像下面那样扩展UITextFieldDelegate。而且,给发件人信息选择器功能是编码的更好选择。

UITextFieldDelegate

Class SomeClass : UITextFieldDelegate {

func textField(_ textField: UITextField, shouldChangeCharactersIn range: NSRange, replacementString string: String) -> Bool {
    // to do
}

}

@objc func textFieldChanged(_ sender : UITextField) {

    // to do

}