iOS 5:如何使用 UIPickerView 创建 UIAlertView? (iPhone / iPad)
我需要一种允许用户从列表中选择选项的方法,我正在考虑在 UIAlertView
中使用 UIPickerView
。但是我想知道是否
- 可以:在
iOS 5
中轻松地将UIPickerView
放入UIAlertView
中(如果是这样,苹果会不赞成吗?) ;应用程序会被拒绝吗?) - 有更好的方法吗? (如果是的话,什么?)
任何帮助或建议将不胜感激!
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:
- It's possible to put a
UIPickerView
in aUIAlertView
easily iniOS 5
(if so is it frowned upon by apple & will be app be rejected?) - Is there a better way of doing this? (if so, what?)
Any help or advice would be really appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您要选择要放入屏幕上文本字段的值,只需让选择器查看文本字段的
inputView
即可。然后它会像键盘一样为您移动到位。您可能还希望将工具栏添加为带有完成、下一步等按钮的
inputAccessoryView
。使选择器视图成为输入视图非常简单。在您的 viewDidLoad 方法中,创建纠察队视图,正常设置其数据源和委托,然后将其分配给文本字段的输入视图属性:
在您的选择器视图委托方法中,您可以在以下情况下使用所选值更新文本字段:选择器的选择已更改。在文本字段委托方法中,您可以确保纠察线在出现之前选择了正确的值(willBeginEditing?(猜测,不是在 mac ATM 上))。
附件视图可以只是一个带有单个完成项的 UIToolbar,该完成项告诉文本字段放弃第一响应者状态。再次设置它并在 viewDidLoad 中分配它。
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:
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.
当然可以将
UIPickerView
添加为UIAlertView
的子视图,我怀疑 Apple 会因此而拒绝您的应用程序,但我认为这会导致 UI 很差。为什么不直接创建一个新的UIViewController
子类来包含UIPickerView
并以模态方式呈现它?这是一种更常见的交互模式。It's certainly possible to add a
UIPickerView
as a subview of aUIAlertView
, 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 newUIViewController
subclass to contain theUIPickerView
and present it modally? That's a much more common interaction pattern.如果您有 2-3 个选择,请将几个按钮放入 UIAlertView 中的 otherButtonTitles: 之后。如果你有更多的选择,你肯定需要构建新的视图控制器。甚至不要尝试将选择器视图添加到 UIAlertView 中。
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) 我相信你可以在任何 iOS SDK 版本中轻松地做到这一点。由于
UIAlertViews
基本上是UIViews
的子类,因此您可以简单地使用addSubview:
2) 这取决于选项列表的上下文。对于生日、日期或时间等选择,Apple 使用单独的
UIViewController
以及UITableView
(一行)和UIPickerView
。如果以上三种都不是,苹果通常会使用UITableView
来在用户的选择旁边打勾。不管怎样,我认为每个人似乎都同意你应该打开带有选项列表的第二页。
1) You can easily do this in any of the iOS SDK versions, I believe. Since
UIAlertViews
are basically subclassedUIViews
, you can simply useaddSubview:
2) This depends the context of the options list. For choices like birthdays or date or time, Apple uses a separate
UIViewController
with aUITableView
(one row) and aUIPickerView
. If it's none of the three above, Apple generally seems to use aUITableView
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.