gpt4 book ai didi

ios - UITextField 点击时没有响应?

转载 作者:行者123 更新时间:2023-11-29 05:38:03 26 4
gpt4 key购买 nike

我有一个UIImageview,它覆盖了主VC的整个View,然后我在imageView上添加了一个UIView,然后我在 View 中添加了两个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的屏幕短片, StoryBoard screen short

最佳答案

我在您的代码中看不到 UITextField 的委托(delegate)。我假设你应该像下面一样扩展 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

}

关于ios - UITextField 点击时没有响应?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56833963/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com