我试图在一个页面上使用多个UIPickers。我想我已经配置了几乎所有东西。使用UIPicker时,我会在工具栏中添加“取消”和“完成”按钮。

工具栏是使用相同的功能构建的,因此当用户点击“完成”时,它将调用相同的操作。下面是一个伪示例流程。

 let pickerData: [String] = ["1", "2", "3", "4"]
 var picker = UIPickerView()
 let pickerData2: [String] = ["A", "B", "C", "D"]
 var picker2 = UIPickerView()

 @IBOutlet weak var textField: UITextField!
 @IBOutlet weak var textField2: UITextField!

override func viewDidLoad() {
  super.viewDidLoad()
  // set up pickers
  setPickerView()
}

// set the pickerview
func setPickerView(){
    picker.dataSource = self
    picker.delegate = self
    textField.inputView = picker
    textField.inputAccessoryView = initToolBar()
    // second picker
    picker2.dataSource = self
    picker2.delegate = self
    textField2.inputView = picker
    textField2.inputAccessoryView = initToolBar()
}

// build the toolbar for uipicker, so a user can select a value
func initToolBar() -> UIToolbar {
    let toolBar = UIToolbar()
    toolBar.barStyle = UIBarStyle.default
    toolBar.isTranslucent = true
    toolBar.tintColor = UIColor(red:14.0/255, green:122.0/255, blue:254.0/255, alpha: 1)
    toolBar.sizeToFit()
    // TODO need to update actions for all buttons
    let cancelButton = UIBarButtonItem(title: "Cancel", style: UIBarButtonItemStyle.plain, target: self, action: nil)
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(pickerDoneAction))
    let spaceButton = UIBarButtonItem(barButtonSystemItem: UIBarButtonSystemItem.flexibleSpace, target: nil, action: nil)
    toolBar.setItems([cancelButton, spaceButton, doneButton], animated: false)
    toolBar.isUserInteractionEnabled = true

    return toolBar
}

func pickerDoneAction(sender: UIBarButtonItem){
     /*Here I'm just updating the same textfield when the done button is
       is pressed, I'm not sure how I can get the instance of the picker into this method*/
     let indexPath = picker.selectedRow(inComponent: 0)
     textfield.text = data[indexPath]
}

// delegates
func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 }
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
  if pickerView == picker {
    return pickerData.count
    } else return pickerData2.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
  if pickerView == picker {
    return pickerData[row]
  } else return pickerData2[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
  if pickerView == picker {
    textField.text = pickerData[row]
  } else textField2.text = pickerData2[row]
}

有没有一种方法可以确定单击完成按钮时正在使用哪个UIPicker,因此我可以在pickerDoneAction方法中使用该实例,然后更改相应的文本字段的值?我尝试检查文本字段是否为firstResponder,甚至尝试检查UIPicker本身,但似乎无法实现。我需要改变我的方法吗?

更新
我已经添加了现有的UIPicikerView委托方法。

只是为了澄清一下,我希望当用户按下“完成”时将UIPicker的值分配给正确的文本字段,因此,如果用户想从UIPicker中选择第一行,则会插入该行。

最佳答案

您始终可以使用UIViewtag属性传递数据:

func setPickerView() {
    picker.tag = 1
    textField.inputView = picker
    textField.inputAccessoryView = initToolBar(pickerTag: picker.tag)

    picker2.tag = 2
    textField.inputView = picker2
    textField.inputAccessoryView = initToolBar(pickerTag: picker2.tag)
}

func initToolBar(pickerTag: Int) {
    let doneButton = UIBarButtonItem(title: "Done", style: UIBarButtonItemStyle.plain, target: self, action: #selector(pickerDoneAction))
    doneButton.tag = pickerTag
}

func pickerDoneAction(sender: UIBarButtonItem){
    let pickerView: UIPickerView

    switch sender.tag {
    case picker.tag:        pickerView = picker
    case picker2.tag:       pickerView = picker2
    default:                return
    }
}

09-25 16:25