我有一个带有三个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时,键盘将打开,而不是选择器视图。当我点击屏幕上其他任何位置时,弹出选择器视图。因此,我的假设是引用EditingDidEnd事件而不是EditingDidBegin。

如何检查并确认?

最佳答案

这是例子

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

关于ios - UIPickerView-不适用于UITextView TouchUp,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/47565152/

10-14 19:58