pickerView 来自 TextField 文本的预定义行

问题描述

我有一些从数据库中填充的文本字段,我想打开 pickerView 并选择与每个文本字段对应的预定义行。 例如来自 ODS 的组件 0,来自 ODC 的组件 1,来自 ODA 的组件 2,来自 ODADD 的组件 3 我可以在 viewDidLoad 中使用“pickerViewNumber.selectRow(5,inComponent: 0,animation: true)”在开始时进行预定义,但我想将它与文本字段中的文本同步。

任何帮助将不胜感激。

import UIKit

class Page2ViewController: UIViewController,UITextFieldDelegate{
 var sphList:[Double] = []
    var cylList:[Double] = []
    var axisList:[Int] = []
    var addList:[Double] = []
    var pickerViewNumber = UIPickerView()

    override func viewDidLoad() {
        super.viewDidLoad()
        ODS.inputView = pickerViewNumber
        ODC.inputView = pickerViewNumber
        ODA.inputView = pickerViewNumber
        ODAdd.inputView = pickerViewNumber
        OSS.inputView = pickerViewNumber
        OSC.inputView = pickerViewNumber
        OSA.inputView = pickerViewNumber
        OSAdd.inputView = pickerViewNumber
        axisList.removeAll()
        for n in 1...180 {
            axisList.append(n)
        }
        sphList.removeAll()
        var sph = -20.25
        for _ in 1...161 {
            sph += 0.25
            sphList.append(sph)
        }
        cylList.removeAll()
        var cyl = -8.25
        for _ in 1...63 {
            cyl += 0.25
            cylList.append(cyl)
        }
        addList.removeAll()
        var add = 0.00
        for _ in 1...16 {
            add += 0.25
            addList.append(add)
        }
}

}
extension Page2ViewController: UIPickerViewDelegate,UIPickerViewDataSource{
  
    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 4
    }
    func pickerView(_ pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int {
        if component == 0{
            return sphList.count
        }
        if component == 1{
            return cylList.count
        }
        if component == 2{
            return axisList.count
        }
        return addList.count
    }
    func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String? {
        if component == 0{
            return numberFormatter.string(for: sphList[row])
        }
        if component == 1{
            return numberFormatter.string(for: cylList[row])
        }
        if component == 2{
            return String(axisList[row])
        }
        return numberFormatter.string(for: addList[row])
        
    }
    func pickerView(_ pickerView: UIPickerView,didSelectRow row: Int,inComponent component: Int) {
        let sph = sphList[pickerViewNumber.selectedRow(inComponent: 0)]
        let cyl = cylList[pickerViewNumber.selectedRow(inComponent: 1)]
        let axis = axisList[pickerViewNumber.selectedRow(inComponent: 2)]
        let add = addList[pickerViewNumber.selectedRow(inComponent: 3)]
    
        if ODS.isEditing{
        ODS.text =  numberFormatter.string(for: sph)
        ODC.text = numberFormatter.string(for:cyl)
        ODA.text = String(axis)
        ODAdd.text = numberFormatter.string(for: add)
        }else if OSS.isEditing{
            OSS.text = numberFormatter.string(for: sph)
            OSC.text = numberFormatter.string(for: cyl)
            OSA.text = String(axis)
            OSAdd.text = numberFormatter.string(for: add)
        }
       // ODS.resignFirstResponder()
    }
    
    
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)