没有调用 textField 代表
Posted
技术标签:
【中文标题】没有调用 textField 代表【英文标题】:textField delegates are not being called 【发布时间】:2017-05-01 05:51:21 【问题描述】:下面提到的是我的 textField 委托方法,我使用 IQKeyBoardSwift 作为智能键盘。 我尝试移除我的键盘,但我的任何方法都没有接到电话接受“开始触摸”
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
【问题讨论】:
所有 IBOutlets 都已连接 textFieldShouldBeginEditing 这个方法叫吗? 不,它没有被调用。我想限制用户在文本字段中输入多个字符 任何调用的委托方法? 仅开始触摸 【参考方案1】:添加:
添加协议UITextFieldDelegate
并尝试替换textField1?.delegate = self as UITextFieldDelegate
替换
这不起作用:
func textFieldDidBeginEditing(textField: UITextField)
这可行:
func textFieldDidBeginEditing(_textField: UITextField)
用于跳转文本字段
见:https://***.com/a/18711677/3901620
【讨论】:
你签入模拟器了吗?【参考方案2】:添加 textFiled 委托:
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
【讨论】:
@RajatAttri textField 与故事板中的委托的连接。以上是关于没有调用 textField 代表的主要内容,如果未能解决你的问题,请参考以下文章
为 UITextField 设置文本后无法调用 shouldChangeCharactersInRange
SwiftUI如何让绑定到同一个状态的多个TextField呈现出不同输入行为