在 iPhone 应用程序中展开和缩回下拉列表
我正在开始开发 iPhone 应用程序。我需要提供一个下拉框,供客户选择一个值,该值本质上在提交时会发送到下游数据库。我显然使用由 IB 构建的 UIPickerView
作为下拉列表。
加载视图后,下拉列表将启用,并显示所有值。
问题是:
- 我只能在单击下拉列表后才展开它吗?
- 用户选择值后我可以撤回列表吗?
我认为下拉菜单非常以网络为中心,但我可能全错了。
I'm starting off developing an iPhone app. I need to present a drop down box for the customer to pick a value which will essentially be sent downstream to a database upon submit. I'm clearly using a UIPickerView
for the drop down list, built from IB.
Once my view loads, the Drop down list is enabled, with all values showing.
Question is:
- Can I only expand the drop down list once it's clicked?
- Can I retract the list once the user selects a value?
I'm thinking very web-centric in terms of drop-downs, but I could have this all wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您有几个选择:
You have a couple options:
您的视图控制器需要实现 UIPickerViewDataSource 以及 UIPickerViewDelegate。在 UIPickerView 的 XIB 中,您需要将文件的所有者与 UIPickerView 的委托和数据源链接起来。
就像在 UITabelDataSource 中一样,您需要在 [numberOfComponentsInPickerView] 内提供组件数量(表:部分)。然后,您需要在[numberOfRowsInComponent]中提供每个组件的行数。最后,您需要为 [titleForRow] 中的每个条目提供标题。
现在,为了让一切变得生动起来,如果选择了上一个组件,您可以使用 [didSelectRow] 加载下一个组件。
注意: UIPickerView 有一个小错误,当组件填充/更改时,不会调用 [didSelectRow]。为了使其工作更顺利,我添加了一个“None”条目作为第一个条目,这是一个非选择,不会导致加载下一个组件。
这是一个基本代码:
因为 UIPickerView 占用了大量空间,并且您无法更改它的大小,所以我建议将其放置在另一个 UIView 上(使其余部分透明),并在用户选择分配给的字段时显示它值(UITextField)。选择完成后(选择最后一个值或用户输入按钮时),您让视图消失并将值设置到 UITextField 上。确保 UITextField 无法编辑,并且输入它会使带有选取器视图的视图出现。完成后,请确保您也移动到下一个字段。
Your view controller needs to implement the UIPickerViewDataSource as well as the UIPickerViewDelegate. In the XIB of the UIPickerView you need to link the File's Owner with the UIPickerView's delegate and datasource.
Like in the UITabelDataSource you need to provide the number of components (table: sections) inside [numberOfComponentsInPickerView]. Then you need to provide the number of rows for each component in [numberOfRowsInComponent]. Finally you need to provide the title for the each entry inside [titleForRow].
Now to bring everything to life you can use [didSelectRow] to load the next component if the previous one was selected.
Attention: UIPickerView has a little bug not to call [didSelectRow] when the component is filled / changed. In order to make it work more smoothly I added a "None" entry as the first entry which is a non-selection and does not cause the next component to be loaded.
This is a rudimentary code:
Because the UIPickerView takes away a lot of space and you cannot change it size I would recommend to place it on an additional UIView (make the rest transparent) and display it when the user selected the field that is assigned to the value (UITextField). When the selection is done (either when last value is selected or when the user enter a button) you let the View disappear and set the value onto the UITextField. Make sure that the UITextField cannot be edited and that entering it make the View with the Picker View appear. When done make sure that you also move to the next field.