我有一个UIImageview,它涵盖了主VC的整个视图,然后在imageView上添加了一个UIView,然后在视图中添加了两个UITextField,如下图所示。
现在
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()
}
答案 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
}