ios 如何快速确定哪个文本字段处于活动状态
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30918732/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to determine which textfield is active swift
提问by Shawjak3
I cant figure out which UITextFieldis currently active so i can clear its text if they hit cancel on a UIBarButtonItem. Here is my code. There is a view with a UIPickerView,UIToolBarand two bar button items. The cancel item has an action that will clear its text only i cant figure out how to get the correct UITextField.
我无法确定哪个UITextField当前处于活动状态,因此如果他们在UIBarButtonItem. 这是我的代码。有一个带有UIPickerView,UIToolBar和两个栏按钮项的视图。取消项有一个操作,可以清除其文本,但我不知道如何获得正确的UITextField.
I've tried these both:
我试过这两个:
@IBAction func cancelText(sender: AnyObject) {
if self.truckYear.editing == true{
println("truckYear")
clearText(self.truckYear)
} else if self.truckMake.editing == true{
clearText(self.truckMake)
}
}
@IBAction func doneText(sender: AnyObject) {
pickerView.hidden = true
}
func clearText(textField:UITextField){
println("in clear text")
textField.text = nil
}
And:
和:
@IBAction func cancelText(sender: AnyObject) {
if self.truckYear.isFirstResponder(){
println("truckYear")
clearText(self.truckYear)
} else if self.truckMake.isFirstResponder(){
clearText(self.truckMake)
}
}
@IBAction func doneText(sender: AnyObject) {
pickerView.hidden = true
}
func clearText(textField:UITextField){
println("in clear text")
textField.text = nil
}
回答by Randy
You can declare a UITextFieldproperty in your class, and assign the current text field to it in textFieldDidBeginEditing.
Then you can just call this text field whenever you need to.
您可以UITextField在类中声明一个属性,并在textFieldDidBeginEditing. 然后,您可以在需要时调用此文本字段。
class ViewController : UIViewController, UITextFieldDelegate {
var activeTextField = UITextField()
// Assign the newly active text field to your activeTextField variable
func textFieldDidBeginEditing(textField: UITextField) {
self.activeTextField = textField
}
// Call activeTextField whenever you need to
func anotherMethod() {
// self.activeTextField.text is an optional, we safely unwrap it here
if let activeTextFieldText = self.activeTextField.text {
print("Active text field's text: \(activeTextFieldText)")
return;
}
print("Active text field is empty")
}
}
回答by Reimond Hill
In swift 4:
在快速 4:
you can get the active UITextField like that
你可以像这样获得活动的 UITextField
extension UIView {
func getSelectedTextField() -> UITextField? {
let totalTextFields = getTextFieldsInView(view: self)
for textField in totalTextFields{
if textField.isFirstResponder{
return textField
}
}
return nil
}
func getTextFieldsInView(view: UIView) -> [UITextField] {
var totalTextFields = [UITextField]()
for subview in view.subviews as [UIView] {
if let textField = subview as? UITextField {
totalTextFields += [textField]
} else {
totalTextFields += getTextFieldsInView(view: subview)
}
}
return totalTextFields
}}
and then you call it like that
然后你这样称呼它
let current = view.getSelectedTextField()
回答by Vyacheslav
In order to improve @Reimond's answer Swift 4
为了改进@Reimond 的答案Swift 4
extension UIView {
var textFieldsInView: [UITextField] {
return subviews
.filter ({ !(optional func textFieldDidEndEditing(_ textField: UITextField)
is UITextField) })
.reduce (( subviews.compactMap { /// Finds the textField that is the first responder in a view hierarchy
func findActiveTextField(in subviews: [UIView], textField: inout UITextField?) {
guard textField == nil else { return }
for view in subviews {
if let tf = view as? UITextField, view.isFirstResponder {
textField = tf
break
}
else if !view.subviews.isEmpty {
findActiveTextField(in: view.subviews, textField: &textField)
}
}
}
// Usage
var aView: UIView = UIView()
var activeField : UITextField?
findActiveTextField(in: aView.subviews, textField: &activeField)
as? UITextField }), { summ, current in
return summ + current.textFieldsInView
})
}
var selectedTextField: UITextField? {
return textFieldsInView.filter { ##代码##.isFirstResponder }.first
}
}
usage:
用法:
view.selectedTextField
view.selectedTextField
回答by Reza Ramezani
you can assign a tag to all your text feild and in textFieldDidBeginEditing check your current textFeild.tag and it's clear witch textfeild is in use .
您可以为所有文本字段分配一个标签,并在 textFieldDidBeginEditing 检查您当前的 textFeild.tag 并且很明显正在使用女巫 textfeild 。
回答by aBikis
I wrote a library to handle basic Form Creation Swift Pop Form.
It doesn't use tagsbut rather sets a datasource that contains fields and uses indexPathForCellto query which is the active textfield. Check out the source code for more!
我写了一个库来处理基本的 Form Creation Swift Pop Form。它不使用tags而是设置一个包含字段的数据源,并用于indexPathForCell查询哪个是活动文本字段。查看源代码了解更多!
回答by Almo
Use the delegate:
使用委托:
##代码##This will tell you when the active textfield finishes editing, and textFieldwill contain a pointer to the one that's no longer going to be active.
这将告诉您活动文本字段何时完成编辑,并且textField将包含一个指向不再处于活动状态的文本字段的指针。

