简体   繁体   中英

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!

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.

It's certainly possible to add a UIPickerView as a subview of a UIAlertView , and I doubt Apple would reject your app because of it, but I think it would make for a poor UI. Why not just create a new UIViewController subclass to contain the UIPickerView and present it modally? That's a much more common interaction pattern.

If you have 2-3 choices put several buttons into UIAlertView after otherButtonTitles:. If you have more choices, definitely you need to build new view controller. Don't even try to add picker view into UIAlertView.

1) You can easily do this in any of the iOS SDK versions, I believe. Since UIAlertViews are basically subclassed UIViews , you can simply use addSubview:

2) This depends the context of the options list. For choices like birthdays or date or time, Apple uses a separate UIViewController with a UITableView (one row) and a UIPickerView . If it's none of the three above, Apple generally seems to use a UITableView which puts a checkmark next to the person's choice.

Either way, I think everyone seems to agree that you should bring up a second page with the list of options.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM