当在swift3中使用pickerviiew时,textFieldDidEndEditing不会触发

时间:2017-12-05 05:47:33

标签: ios swift3 delegates uitextfield uipickerview

在Swift 3中使用选择器视图时,文本字段委托方法textFieldDidEndEditing未触发。如何调用它?没有动态调用委托的按钮。

您可以在下面看到我的代码。

image here

import UIKit

class ThroewViewController: UIViewController,UIPickerViewDelegate,UIPickerViewDataSource,UITextFieldDelegate {



    @IBOutlet weak var pickerText: UITextField!


    let thePicker = UIPickerView()
    var dashTitle_Arr = ["One","Two","Three","Four","Five","Six"]

    override func viewDidLoad() {
        super.viewDidLoad()

        pickerText.delegate = self
       thePicker.dataSource = self
        thePicker.delegate = self
       pickerText.inputView = thePicker



    }


    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {

        return dashTitle_Arr .count
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {


        return dashTitle_Arr [row]     }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {


        pickerText.text = dashTitle_Arr [row]

    }

    func textFieldDidEndEditing(_ textField: UITextField) {
        addIntoArray(text: textField.text)
    }

    func addIntoArray(text: String?) {

        guard let text = text, text.characters.count > 0 else {

            return
        }

        dashTitle_Arr.append(text)
        thePicker.reloadAllComponents()
    }
}

1 个答案:

答案 0 :(得分:3)

picker-view的属性为accessoriesView。使用tool bardone按钮创建cancel,并在选择器视图中设置assessoriesView

在工具栏button action中写下textfield.resignFirstResponder() 之后,textFieldDidEndEditing委托方法被解雇

对我来说很好。希望它能帮到你。谢谢