简体   繁体   English

在选择时关闭作为 UITextField 输入视图的 UIPickerView

[英]Close UIPickerView that is an UITextField inputview on selection

I'm trying to close my UIPickerView when a selection was made:进行选择时,我试图关闭我的 UIPickerView:

    let pickerElement = UIPickerView()
    let textfieldElement = UITextField()

    textfieldElement.inputView = pickerElement
    pickerElement.delegate = self
    pickerElement.dataSource = self

    // ...
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        textfieldElement.text = options[row].label as? String
        selectedElementId = options[row].id
        // this is not doing anything
        pickerElement.resignFirstResponder()
        pickerView.resignFirstResponder()
        
        // leaves the grey view open
        pickerElement.isHidden = true
        pickerView.isHidden = true
    }

This does not seem to work, it leaves open the grey view at the bottom:这似乎不起作用,它在底部打开灰色视图: 在此处输入图像描述

You need to resign the text field instead of the picker or anything else.您需要放弃文本字段而不是选择器或其他任何东西。

Add this添加这个

textfieldElement.resignFirstResponder()

or you can also use或者你也可以使用

self.view.endEditing(true)

Note: Remove all other things like resign to other views or hide.注意:删除所有其他内容,例如辞职到其他视图或隐藏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM