【发布时间】: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 这个方法叫吗?
-
不,它没有被调用。我想限制用户在文本字段中输入多个字符
-
任何调用的委托方法?
-
仅开始触摸
标签: ios xcode swift3 uitextfield uitextfielddelegate