UIPicker - 如果用户只单击确定按钮,如何记录标准的 selectedRow

UIPicker - how record the standard selectedRow if user only click the OK button

我有一个模式 window 的 UIpicker。一切正常,除了一件事。当用户单击输入文本字段时,带有 UIpickerView 的模态 window 按预期打开。如果 selected 行是正确的并且用户单击了确定按钮,则当前代码无法注册该值。

// Set the default row, so if user click ok without changing it is recorded
self.picker.selectRow(0, inComponent: 0, animated: false)

是否有 didSelectRow 的替代方案?我认为这是问题所在,因为他们的用户不会 select 一行,因为代码已经预先 select。

// Capture the picker selection
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    // This method is triggered whenever the user makes a change to the picker selection.
    // The parameter named row and component represents what was selected.
    selectedType = pickerData[row] as String
}

很好解决。只需添加一个测试来查看 selectedType 是否为 nil。

@IBAction func OkBtnWasPressed(_ sender: Any) {
        // update text to selected value
        if selectedType == nil {
            typeTextField.text = pickerData[defaultRow]
        } else {
             typeTextField.text = selectedType
        }
        animateOut()
    }