没有调用 textField 代表
textField delegates are not being called
下面提到的是我的 textField 委托方法,我正在使用 IQKeyBoardSwift 作为智能键盘。
我尝试移除我的键盘,但我仍然没有收到关于我的任何方法的电话接受 'begins touch'
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == textField {
textFIeld2.becomeFirstResponder()
}else if textField == textFIeld2 {
textField3.becomeFirstResponder()
}else if textField == textField3 {
textFIeld4.becomeFirstResponder()
}else if textField == textFIeld4 {
textFIeld4.resignFirstResponder()
}
return true
}
// Resign first reponder on touch
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.location(in: view)
if !textField1.frame.contains(location) {
textField1.resignFirstResponder()
}
if !textFIeld2.frame.contains(location) {
textFIeld2.resignFirstResponder()
}
if !textField3.frame.contains(location) {
textField3.resignFirstResponder()
}
if !textFIeld4.frame.contains(location) {
textFIeld4.resignFirstResponder()
}
self.textField1.endEditing(true)
self.textFIeld2.endEditing(true)
self.textField3.endEditing(true)
self.textFIeld4.endEditing(true)
}
//MARK: UITextFieldDelegate
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
print("textFieldShouldBeginEditing")
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
print("textFieldDidBeginEditing")
print("Leaving textFieldDidBeginEditing")
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
if(textField == textField1)
{
if(newString.characters.count) == 1 {
print(textField1)
textFIeld2.becomeFirstResponder()
return false
}
}
if(textField == textFIeld2)
{
if(newString.characters.count) == 1{
textField3.becomeFirstResponder()
return false
}
}
if(textField == textField3)
{
if(newString.characters.count) == 1{
textFIeld4.becomeFirstResponder()
return false
}
}
if(textField == textFIeld4)
{
if(newString.characters.count) == 1{
buttonOutletSubmitCode.isHidden = false
resignFirstResponder()
return false
}
}
return true
}
我希望我的光标在收到 1 个字符的输入后跳转到下一个文本字段。不幸的是,这并没有发生。
override func viewDidLoad() {
super.viewDidLoad()
textField1.delegate = self as? UITextFieldDelegate
textFIeld2.delegate = self as? UITextFieldDelegate
textField3.delegate = self as? UITextFieldDelegate
textFIeld4.delegate = self as? UITextFieldDelegate
}
这是我的viewDidLoad
添加文本字段委托:
class ViewController: UIViewController , UITextFieldDelegate{
是的,这不是调用,因为您的代表是旧 swift 版本。替换它 Swift 3 代码。
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
if textField == textField {
textFIeld2.becomeFirstResponder()
}else if textField == textFIeld2 {
textField3.becomeFirstResponder()
}else if textField == textField3 {
textFIeld4.becomeFirstResponder()
}else if textField == textFIeld4 {
textFIeld4.resignFirstResponder()
}
return true
}
添加:
添加协议UITextFieldDelegate
并尝试替换textField1?.delegate = self as UITextFieldDelegate
替换
这个不工作:
func textFieldDidBeginEditing(textField: UITextField) {
}
有效:
func textFieldDidBeginEditing(_textField: UITextField) {
}
用于跳转textField
参见:
下面提到的是我的 textField 委托方法,我正在使用 IQKeyBoardSwift 作为智能键盘。 我尝试移除我的键盘,但我仍然没有收到关于我的任何方法的电话接受 'begins touch'
func textFieldShouldReturn(textField: UITextField) -> Bool {
if textField == textField {
textFIeld2.becomeFirstResponder()
}else if textField == textFIeld2 {
textField3.becomeFirstResponder()
}else if textField == textField3 {
textFIeld4.becomeFirstResponder()
}else if textField == textFIeld4 {
textFIeld4.resignFirstResponder()
}
return true
}
// Resign first reponder on touch
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
let touch = touches.first! as UITouch
let location = touch.location(in: view)
if !textField1.frame.contains(location) {
textField1.resignFirstResponder()
}
if !textFIeld2.frame.contains(location) {
textFIeld2.resignFirstResponder()
}
if !textField3.frame.contains(location) {
textField3.resignFirstResponder()
}
if !textFIeld4.frame.contains(location) {
textFIeld4.resignFirstResponder()
}
self.textField1.endEditing(true)
self.textFIeld2.endEditing(true)
self.textField3.endEditing(true)
self.textFIeld4.endEditing(true)
}
//MARK: UITextFieldDelegate
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
print("textFieldShouldBeginEditing")
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
print("textFieldDidBeginEditing")
print("Leaving textFieldDidBeginEditing")
}
func textField(textField: UITextField, shouldChangeCharactersInRange range: NSRange, replacementString string: String) -> Bool {
let newString = NSString(string: textField.text!).replacingCharacters(in: range, with: string)
if(textField == textField1)
{
if(newString.characters.count) == 1 {
print(textField1)
textFIeld2.becomeFirstResponder()
return false
}
}
if(textField == textFIeld2)
{
if(newString.characters.count) == 1{
textField3.becomeFirstResponder()
return false
}
}
if(textField == textField3)
{
if(newString.characters.count) == 1{
textFIeld4.becomeFirstResponder()
return false
}
}
if(textField == textFIeld4)
{
if(newString.characters.count) == 1{
buttonOutletSubmitCode.isHidden = false
resignFirstResponder()
return false
}
}
return true
}
我希望我的光标在收到 1 个字符的输入后跳转到下一个文本字段。不幸的是,这并没有发生。
override func viewDidLoad() {
super.viewDidLoad()
textField1.delegate = self as? UITextFieldDelegate
textFIeld2.delegate = self as? UITextFieldDelegate
textField3.delegate = self as? UITextFieldDelegate
textFIeld4.delegate = self as? UITextFieldDelegate
}
这是我的viewDidLoad
添加文本字段委托:
class ViewController: UIViewController , UITextFieldDelegate{
是的,这不是调用,因为您的代表是旧 swift 版本。替换它 Swift 3 代码。
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool{
if textField == textField {
textFIeld2.becomeFirstResponder()
}else if textField == textFIeld2 {
textField3.becomeFirstResponder()
}else if textField == textField3 {
textFIeld4.becomeFirstResponder()
}else if textField == textFIeld4 {
textFIeld4.resignFirstResponder()
}
return true
}
添加:
添加协议UITextFieldDelegate
并尝试替换textField1?.delegate = self as UITextFieldDelegate
替换
这个不工作:
func textFieldDidBeginEditing(textField: UITextField) {
}
有效:
func textFieldDidBeginEditing(_textField: UITextField) {
}
用于跳转textField
参见: