Confirming which uipicker is used from toolbar "done" press

I am trying to use multiple UIPickers on a page. I think I’ve configured almost everything. When I use a UIPicker I add in a a tool bar with a “Cancel” and a “Done” Button. I did this so that a user’s input including the very first option is always inserted into the textfield (The textfield inputView is the picker).

On the toolBar (They are all built using the same method) my DoneButton has an action called PickerDoneAction. It looks like this:

func pickerDoneAction(sender: UIBarButtonItem){
     let indexPath = picker.selectedRow(inComponent: 0)
     textfield.text = data[indexPath]
} 

How would I determine which UIPicker was being used when the done button was clicked, so I could then change the textfield in question and it’s data source? I tried checking if the textfield was firstResponder and even tried to check the UIPicker itself. Should I rethink my approach?

@jonny. Thanks very much for your question, and my apologies for the delayed response! What you can do is set the “tag” property of each Picker to a particular integer (1, 2, 3, etc). Then you would check the picker’s tag value inside an if/else clause:

e.g. if picker.tag == 1 {
//do something
} else {
//do something else
}

And that is perhaps the easiest way to check one picker against another. :slight_smile:

I hope this helps!

All the best!

1 Like