单击Button时,UITextField不会结束编辑(委托textFieldDidEndEditing) [英] UITextField doesn't end editing when Button clicked( delegate textFieldDidEndEditing )
问题描述
我在屏幕上有两个textFields
和一个Submit button
.用户在第一个textField中输入详细信息,然后在第二个textField中输入.
I have two textFields
on the screen and a Submit button
. User inputs details in first textField and then the second one.
我的要求是在单击Submit button
时结束编辑,并在这些textFields
中打印用户输入.我在打印第二个textField的值时遇到问题,因为当用户单击<时编辑似乎永远不会结束c1>.
My requirement is to end the editing when Submit button
is clicked and print the user inputs in these textFields
.I am having issues printing the second textField's value, as the editing never seems to end when the user clicks the Submit button
.
这是我的代码.感谢您在此问题上的帮助(我已添加文本字段委托)
Here is my code. Appreciate your help on this issue (I have added the textfield delegate)
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
var firstName = ""
var lastName = ""
@IBOutlet var buttonUI: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func Submit(sender: UIButton) {
print(firstName)
print(lastName)
}
func textFieldDidEndEditing(textField: UITextField) {
switch textField.tag {
case 1:
firstName = textField.text!
print(firstName)
case 2:
lastName = textField.text!
print(lastName)
default: break
}
}
}
推荐答案
在您的ViewController
类中,对于每个文本字段,通过将故事板中的每个文本字段Ctrl拖动到代码中,来创建@IBOutlet属性
Within your ViewController
class, for each textfield, create an @IBOutlet property by Ctrl-dragging each text field from the storyboard to your code
@IBOutlet weak var textFieldFirst: UITextField!
@IBOutlet weak var textFieldSecond: UITextField!
还要创建一个私有的UITextField
属性,以保存(可能是)最新(/最近使用)的文本字段.
Also create a private UITextField
property to hold the (possibly) current (/most recently used) text field.
private var currentTextField: UITextField?
重写您的UIViewController
子类的viewDidLoad
方法,以初始化两个公共"文本字段实例的委托
Override the viewDidLoad
method of your UIViewController
subclass to initialize the delegates for the two "public" text field instances
override func viewDidLoad() {
super.viewDidLoad()
// Handle the user input in the text fields through delegate callbacks (and set tags)
textFieldFirst.delegate = self
textFieldFirst.tag = 1
textFieldSecond.delegate = self
textFieldSecond.tag = 2
}
此外,使用UITextFieldDelegate
的两个textFieldShouldReturn(...)
和textFieldDidBeginEditing(...)
方法分别将(活动)文本字段状态声明为第一响应者,并更新currentTextField
引用.
Moreover, use the two textFieldShouldReturn(...)
and textFieldDidBeginEditing(...)
methods of the UITextFieldDelegate
to resign the (active) text fields status's as first responder and to update the currentTextField
reference, respectively.
// UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
// User finished typing (hit return): hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
currentTextField = textField
}
最后,在编辑过程中按下submit
的情况下,请将任何可能的当前文本字段都放弃作为第一响应者
Finally, resign any possibly current text field as first responder, in case submit
is pressed in-middle-of editing
@IBAction func Submit(sender: UIButton) {
if let currentTextField = currentTextField {
currentTextField.resignFirstResponder()
}
print(firstName)
print(lastName)
}
有了这个,您的视图控制器应该可以正常工作了.
With this, your view controller should work as intended.
总结:在进行了上述添加和修改之后,您的ViewController
类应该看起来像
To summarize: after the additions and modifications above, your ViewController
class should look like
import UIKit
class ViewController: UIViewController, UITextFieldDelegate {
// Properties
@IBOutlet weak var textFieldFirst: UITextField!
@IBOutlet weak var textFieldSecond: UITextField!
private var currentTextField: UITextField?
var firstName = ""
var lastName = ""
@IBOutlet var buttonUI: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
}
// Actions
@IBAction func Submit(sender: UIButton) {
if let currentTextField = currentTextField {
currentTextField.resignFirstResponder()
}
print(firstName)
print(lastName)
}
// viewDidLoad
override func viewDidLoad() {
super.viewDidLoad()
// handle the user input in the text fields through delegate callbacks
textFieldFirst.delegate = self
textFieldSecond.delegate = self
// tags
textFieldFirst.tag = 1
textFieldSecond.tag = 2
}
// UITextFieldDelegate
func textFieldShouldReturn(textField: UITextField) -> Bool {
// User finished typing (hit return): hide the keyboard.
textField.resignFirstResponder()
return true
}
func textFieldDidBeginEditing(textField: UITextField) {
currentTextField = textField
}
func textFieldDidEndEditing(textField: UITextField) {
switch textField.tag {
case 1:
firstName = textField.text!
print(firstName)
case 2:
lastName = textField.text!
print(lastName)
default: break
}
}
}
这篇关于单击Button时,UITextField不会结束编辑(委托textFieldDidEndEditing)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!