很难判断此处所问的问题。此问题模棱两可、模糊不清、不完整、过于宽泛或过于修辞,无法以其当前形式合理地回答。若要获得澄清此问题以便重新打开的帮助,请单击visit the help center。
10年前关闭了。
我正在设置一个UIPickerView
,使其具有choice a、choice b、choice c等选项。我尝试解释Apple的示例代码,但对于像我这样的初学者来说,这似乎很难理解。如果可能的话,我还希望从选择器视图中的选项将我带到另一个页面。
很难判断此处所问的问题。此问题模棱两可、模糊不清、不完整、过于宽泛或过于修辞,无法以其当前形式合理地回答。若要获得澄清此问题以便重新打开的帮助,请单击visit the help center。
10年前关闭了。
我正在设置一个UIPickerView
,使其具有choice a、choice b、choice c等选项。我尝试解释Apple的示例代码,但对于像我这样的初学者来说,这似乎很难理解。如果可能的话,我还希望从选择器视图中的选项将我带到另一个页面。
3条答案
按热度按时间idv4meu81#
对于每个初学者来说,第一次理解这些东西显然是有些乏味的。
无论如何,您知道如何使用
UITableView
吗?您知道如何使用UITableViewDelegate
和UITableViewDataSource
吗?如果您的答案是肯定的,那么就想象UIPickerView
与UITableView
一样(但请记住它们不是UITableViewController
)。比如说,我有一个
UIPickerView
:1)首先,您需要通过IB或代码将
delegate
和dataSource
分配给UIPickerView
,这取决于您的实现(因此,这一步看起来非常类似于UITableView
,不是吗?)就像这样:
2)接下来,您需要定义截面的数量,如下所示:
2)然后,您需要定义所需的行数:
3)然后,定义行标题(如果有多个部分,则为每个部分定义标题):
4)接下来,当有人点击某个元素时,您需要获取事件(当您想要导航到其他控制器/屏幕时):
这就是您所需要的全部实现。
n8ghc7c12#
我将简要说明如何通过编程实现这一点
基本上,在
viewDidLoad
中,我们将创建一个UIPickerView
并将其添加到视图中,告诉它我们的控制器同时用作delegate
和dataSource
(您也可以在Interface Builder中完成此操作)然后,我们将实现两个数据源方法,分别为
numberOfComponentsinPickerView:
和pickerView:numberOfRowsInComponent:
,以了解pickerView有多少组件以及每个组件有多少行最后,我们实现了返回每一行内容的委托方法
pickerView:titleForRow:forComponent:
。如果您想自定义选中一行时的行为,可以实现委托方法
pickerView:didSelectRow:inComponent:
,顾名思义,每次选中一行时都会调用该方法。mnowg1ta3#
UIPickerView使用与UITableView.
DataSource
和Delegate
协议类似的部分。DataSource
方法用于告诉选择器中有多少“列”和多少“行”。而
Delegate
方法用于其余的,行高,列宽,视图或标题显示和回调时,选择器被移动。用户界面选择器视图