Want UIPickerView to trigger an action alert when touched

I would like for an action alert to appear when the user touches the UIPickerView on the iPhone.


I have tried connecting the picker to my alert @IBAction method in ViewController.swift but it is ignored - I do not get the usual pop-up menu when cntrl-dragging from the UIPicker to the action method. From this I can surmise that this is not possible or I am doing this incorrectly.


I added a test button and tried to connect it to the action method and got the normal 'Connect Action' pop-up menu so from this I am thinking that what I am trying to do is not allowed. My reason for wanting to do this is to provide the user with some advice/info before she uses the picker to set a field in the view.


Thanks!

Answered by junkpile in 81151022

I would suggest you implement the UIPickerViewDelegate method pickerView:didSelectRow:inComponent: and programmatically do whatever you want there.

Accepted Answer

I would suggest you implement the UIPickerViewDelegate method pickerView:didSelectRow:inComponent: and programmatically do whatever you want there.

@ junkpile


Thank you for your suggestion. I will try that out and report back.


Thanks again.

@ junkpile


Bravo!! Your suggestion worked perfectly. However, I had to battle a bizarre Xcode/Swift error message that cost me about two hours or so.


If you add 'UIPickerViewDataSource, UIPickerViewDelegate' to class ViewController: UIViewController, as you must,

you get the following error msg: Type ‘ViewController’ does not conform to protocol ‘UIPickerDataSource’


I know that adding those protocol declarations should not trigger an error but it does. After two hours of coding re-coding I added the following:


func numberOfComponentsInPickerView(pickerView: ...


the error vanished. I understand the strictness of Swift, but this error is ridiculous.


As coded, when the user touches the first component (wheel) the action alert msg pops up and that is precisely what I wanted to happen. I now think that trying to connect the action to the UIPickerView itself would not have done what I wanted because that may have triggered the alert everytime a wheel was touched and that would have been incorrect. The user only needs to get the alert once.


Bottom line: I am now good to go. Thank you very much for your help.

It should indeed trigger an error if the protocol you just claimed you implemented has some required methods.


The only "ridiculous" part (and that's a matter of debate... we get spoiled these days with smart IDEs and forget the way it used to be...) is that there is no fix-it option in Xcode to automatically insert stub implementations of all required protocol methods. You actually have to go read the documentation for the protocol. (the horror 😀)


Anyway, glad you got it working.

@ junkpile


You are correct in all that you noted. I really think that Swift is going to make me a better coder.


Thanks again for all your help.

Want UIPickerView to trigger an action alert when touched
 
 
Q