UIPickerView - 不适用于UITextView TouchUp

时间:2017-11-30 02:53:06

标签: ios swift uitextview uipickerview uialertcontroller

我有一个包含三个UITextView的表单。我想在文本字段上显示UIPickerView。我在下面写了代码 -

@IBAction func selectServiceBook(_ sender: UITextField) {

    let message = "\n\n\n\n\n\n"
    let alert = UIAlertController(title: "Select Service Book", message: message, preferredStyle: UIAlertControllerStyle.alert)
    alert.isModalInPopover = true

    let pickerFrame = UIPickerView(frame: CGRect(x: 20, y: 20, width: 200, height: 140)) // CGRectMake(left, top, width, height) - left and top are like margins
    pickerFrame.tag = 666
    //set the pickers datasource and delegate
    pickerFrame.delegate = self

    //Add the picker to the alert controller
    alert.view.addSubview(pickerFrame)
    let okAction = UIAlertAction(title: "OK", style: .default, handler: {
        (alert: UIAlertAction!) -> Void in
        self.ServiceBook.text = self.selectedServiceBook
    })
    alert.addAction(okAction)
    self.present(alert, animated: true, completion: {
        self.ServiceBook.text = self.selectedServiceBook
    })
}

这可以完成两个字段的工作,但是只需点击ServiceBook就可以打开键盘而不是选择器视图。当我点击屏幕中的任何其他位置时,弹出选择器视图。所以我的假设是不是EditingDidBegin而是指的是EditingDidEnd事件。

我该如何检查并确认?

1 个答案:

答案 0 :(得分:1)

这是示例

let myPickerProject = [ "Project 1", "Project 2", "Project 3", "Project 4", "Project 5", "Project 6"] // to feed row
pickerProject.delegate = self
let pickerProject = UIPickerView()
let txtFiled = UITextField.init(frame: <#T##CGRect#>)
txtFiled.inputView = pickerProject
func numberOfComponents(in pickerView: UIPickerView) -> Int {
     return 1
} 
func pickerView( _ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
     return myPickerProject.count
}

func pickerView( _ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    return myPickerProject[row]
}

func pickerView( _ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let selectedvalue = myPickerProject[row]
}