如何确定哪个文本字段是活跃的

时间:2015-06-18 14:49:41

标签: ios swift

我无法确定哪个UITextField当前有效,因此我可以在UIBarButtonItem点击取消时清除其文字。这是我的代码。有一个包含UIPickerViewUIToolBar和两个条形按钮项的视图。取消项目有一个仅清除其文本的操作我无法弄清楚如何获得正确的UITextField

我试过这两个:

@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
}

@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
}

7 个答案:

答案 0 :(得分:42)

您可以在班级中声明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")
   }
}

答案 1 :(得分:15)

在swift 4中:

你可以像这样获得活跃的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
}}

然后你就这样称呼它

let current = view.getSelectedTextField()

答案 2 :(得分:6)

为了改善@Reimond的答案 Swift 4

extension UIView {
    var textFieldsInView: [UITextField] {
        return subviews
            .filter ({ !($0 is UITextField) })
            .reduce (( subviews.compactMap { $0 as? UITextField }), { summ, current in
                return summ + current.textFieldsInView
        })
    }
    var selectedTextField: UITextField? {
        return textFieldsInView.filter { $0.isFirstResponder }.first
    }
}

用法:

view.selectedTextField

答案 3 :(得分:0)

使用委托:

optional func textFieldDidEndEditing(_ textField: UITextField)

这将告诉您活动文本字段何时完成编辑,textField将包含指向不再处于活动状态的指针。

答案 4 :(得分:0)

/// 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)

答案 5 :(得分:0)

我写了一个库来处理基本的表单创建Swift Pop Form。 它不使用tags,而是设置包含字段的数据源,并使用indexPathForCell来查询哪个是活动文本字段。查看源代码了解更多信息!

答案 6 :(得分:0)

您可以为所有文本字段和 textFieldDidBeginEditing 检查您当前的textFeild.tag,很明显,正在使用textfeild。