This question is not duplicated from these:
这个问题与以下问题不重复:
How to disable/enable the return key in a UITextField?
如何在UITextField中禁用/启用返回键?
How to enable or disable the keyboard return key
如何启用或禁用键盘返回键
Enable and Disable Keyboard return key on demand in iOS
在iOS中按需启用和禁用键盘返回键
I have two TextFields
.
我有两个TextFields。
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
-
textField1
has the Next button like the Return Key;textField1具有Next键,如Return键;
-
textField2
has the Go button like the Return Key;textField2像返回键一样有Go按钮;
I would like to enable the Go button of the second TextField just if both TextFields are not empty.
我想在两个TextField都不为空的情况下启用第二个TextField的Go按钮。
I tried to use someTextField.enablesReturnKeyAutomatically
with TextFieldDelegate
, but did not work.
我尝试使用TextFieldDelegate自动使用someTextField.enablesReturnKey,但是没有用。
Thank you for help.
谢谢你的帮助。
1 个解决方案
#1
6
Below: textField2
is disabled as long as textField1
is empty. If the latter is non-empty, we enable textField2
, but enable the Go
button only if textField2
is non-empty (via .enablesReturnKeyAutomatically
property),
下面:只要textField1为空,textField2就会被禁用。如果后者非空,我们启用textField2,但仅当textField2非空(通过.enablesReturnKeyAutomatically属性)时才启用Go按钮,
/* ViewController.swift */
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// text field delegates
textField1.delegate = self
textField2.delegate = self
// set return key styles
textField1.returnKeyType = UIReturnKeyType.Next
textField2.returnKeyType = UIReturnKeyType.Go
// only enable textField2 if textField1 is non-empty
textField2.enabled = false
// only enable 'go' key of textField2 if the field itself is non-empty
textField2.enablesReturnKeyAutomatically = true
}
// UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
if (textField1.text?.isEmpty ?? true) {
textField2.enabled = false
textField.resignFirstResponder()
}
else if textField == textField1 {
textField2.enabled = true
textField2.becomeFirstResponder()
}
else {
textField.resignFirstResponder()
}
return true
}
}
Runs as follows:
运行如下:
#1
6
Below: textField2
is disabled as long as textField1
is empty. If the latter is non-empty, we enable textField2
, but enable the Go
button only if textField2
is non-empty (via .enablesReturnKeyAutomatically
property),
下面:只要textField1为空,textField2就会被禁用。如果后者非空,我们启用textField2,但仅当textField2非空(通过.enablesReturnKeyAutomatically属性)时才启用Go按钮,
/* ViewController.swift */
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// text field delegates
textField1.delegate = self
textField2.delegate = self
// set return key styles
textField1.returnKeyType = UIReturnKeyType.Next
textField2.returnKeyType = UIReturnKeyType.Go
// only enable textField2 if textField1 is non-empty
textField2.enabled = false
// only enable 'go' key of textField2 if the field itself is non-empty
textField2.enablesReturnKeyAutomatically = true
}
// UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
if (textField1.text?.isEmpty ?? true) {
textField2.enabled = false
textField.resignFirstResponder()
}
else if textField == textField1 {
textField2.enabled = true
textField2.becomeFirstResponder()
}
else {
textField.resignFirstResponder()
}
return true
}
}
Runs as follows:
运行如下: