使用工具栏按钮更改 UIPickerView 值

Posted

技术标签:

【中文标题】使用工具栏按钮更改 UIPickerView 值【英文标题】:Change UIPickerView values with toolbar button 【发布时间】:2021-01-02 07:40:51 【问题描述】:

我有一个带有 2 个组件的 UITableView,供用户以英尺和英寸为单位输入他们的身高。我想在 UIPicker 工具栏内添加一个按钮,该按钮将提供将输入从 2 个组件(英尺和英寸)更改为仅厘米的选项。我已经在 UIPickerView 上添加了工具栏按钮,但是我不知道如何将英尺和英寸从英尺和英寸更改为 cm 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
   
    
   
   

【问题讨论】:

【参考方案1】:

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

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"
        
    

...

【讨论】:

以上是关于使用工具栏按钮更改 UIPickerView 值的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 UIPickerView 更改按钮的操作

使用 UIToolBar 上的完成按钮关闭 UIPickerView

当 UIPickerView 值更改时,表行在 ios 中更改

如何更改 UIpickerView 上的元素按下按钮或按下另一个按钮

Swift - UIPickerView 子视图中没有出现工具栏完成按钮

如何将带有完成按钮的工具栏添加到 UIPickerView?