Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iOS 5: How can I create a UIAlertView with a UIPickerView? (iPhone / iPad)

I need a way of allowing a user to pick on option out of a list, I'm thinking of using a UIPickerView in a UIAlertView. However I was wondering if:

  1. It's possible to put a UIPickerView in a UIAlertView easily in iOS 5 (if so is it frowned upon by apple & will be app be rejected?)
  2. Is there a better way of doing this? (if so, what?)

Any help or advice would be really appreciated!


1 Answers

If you are choosing a value to be put into a text field on the screen, just make your picker view the inputView of the text field. It will then animate into place for you just like the keyboard.

You may also wish to add a toolbar as an inputAccessoryView with done, next etc. buttons on it.

Making the picker view the input view is very simple. In your viewDidLoad method, create the picket view, setting its datasource and delegate as normal, then just assign it to the input view property of the text field:

myTextField.inputView = myPicker;

In your picker view delegate methods, you can update the text field with the selected value when the picker's selection is changed. In your text field delegate methods, you can make sure the picket has the right value selected before it appears (willBeginEditing? (guess, not at the mac ATM)).

The accessory view can just be a UIToolbar with a single done item that tells the text field to resign first responder status. Again, set this up and assign it in viewDidLoad.

like image 87
jrturton Avatar answered Nov 30 '25 05:11

jrturton



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!