使用工具栏按钮更改 UIPickerView 值

Change UIPickerView values with toolbar button

我有一个包含 2 个组件的 UITableView,供用户以英尺和英寸为单位输入他们的身高。我想在 UIPicker 工具栏内添加一个按钮,可以选择将输入从 2 个分量(英尺和英寸)更改为仅厘米。我已经在 UIPickerView 上添加了工具栏按钮,但是我不知道如何将英尺和英寸从英尺和英寸更改为厘米 UIPickerView。

   let heightTF: UITextField = {
      let tf = UITextField()
      tf.placeholder = " Height"
      tf.translatesAutoresizingMaskIntoConstraints = false
      return tf
  }()


var pickerView: UIPickerView!

let feetList = Array(3...9)

let inchList = Array(0...11)

let numberOfComponents = 4


func numberOfComponents(in pickerView: UIPickerView) -> Int {
    return numberOfComponents
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
    if component == 0 {
        return feetList.count
    }else if component == 2 {
        return inchList.count
    }else {
        return 1
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    if component == 0 {
        return "\(feetList[row])"
    }else if component == 1 {
        return "ft"
    }else if component == 2 {
        return "\(inchList[row])"
    }else {
        return "in"
    }
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
    let feetIndex = pickerView.selectedRow(inComponent: 0)
    let inchIndex = pickerView.selectedRow(inComponent: 2)
    heightTF.text = "\(feetList[feetIndex])'\(inchList[inchIndex])''"
}
@objc func donePicker() {

    heightTF.resignFirstResponder()

}

 @objc func cmAction() {

    print("cm pressed")

}
func addDoneButtonOnKeyboard(){
        let doneToolbar: UIToolbar = UIToolbar(frame: CGRect.init(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
        doneToolbar.barStyle = .default
       let cmAction: UIBarButtonItem = UIBarButtonItem(title: "CM", style: .done, target: self, action: #selector(self.cmAction))
        let flexSpace = UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil)
        let done: UIBarButtonItem = UIBarButtonItem(title: "Done", style: .done, target: self, action: #selector(self.doneButtonAction))

        let items = [cmAction, flexSpace, done]
        doneToolbar.items = items
        doneToolbar.sizeToFit()

        heightTF.inputAccessoryView = doneToolbar
   

    }

    @objc func doneButtonAction(){
        heightTF.resignFirstResponder()
    }
override func viewDidLoad() {
    addDoneButtonOnKeyboard()
    
    self.pickerView = UIPickerView(frame:CGRect(x: 0, y: 0, width: self.view.frame.size.width, height: 216))
    self.pickerView.delegate = self
    self.pickerView.dataSource = self
    heightTF.inputView = self.pickerView
    self.pickerView.backgroundColor = UIColor.white
    heightTF.inputView = self.pickerView
   
    
   
   }

首先您需要保存您要使用的单位类型:

enum UnitType {
    case feetInches
    case cm
}
var unitType = UnitType.feetInches

然后在操作中更改 feetInches 和 cm 之间的值,并重新加载 pickerView 的所有组件。

@objc func cmAction() {
    switch unitType {
    case .feetInches:
         // add code to convert from feet/inches to cm
         unitType = .cm
        self.pickerView.reloadAllComponents()
        // add code to select cm row
    case .cm:
         // add code to convert from cm to feet/inches
        unitType = .feetInches
        self.pickerView.reloadAllComponents()
        // add code to select feet and inches rows
    }
}

在您根据unitType 的值设置的每个pickerView 委托方法中。 例如:

func numberOfComponents(in pickerView: UIPickerView) -> Int {
    switch unitType {
    case .feetInches:
        return numberOfComponents /* =4 */
    case .cm:
        return 2 /* (number of cm) and cm */
    }
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
    switch unitType {
    case .feetInches:
        if component == 0 {
            return "\(feetList[row])"
        }else if component == 1 {
            return "ft"
        }else if component == 2 {
            return "\(inchList[row])"
        }else {
        return "in"
        }
    case .cm: 
        if component == 0 {
            return "\(cmList[row])"
        }else if component == 1 {
            return "cm"
        }
    }
}
...