Swift 3.0 多个选择器视图无法识别数据源变量
Posted
技术标签:
【中文标题】Swift 3.0 多个选择器视图无法识别数据源变量【英文标题】:Swift 3.0 Multiple Picker Views not recognizing Data Source Variable 【发布时间】:2017-05-12 04:25:25 【问题描述】:我的目标是在一个 viewController 上拥有 3 个pickerView。每个pickerView 将根据以前保存的设置列出不同的数据源,这些设置是从数据库中提取的。我已经根据使用如下所示的 switch 语句从数据库中提取的值设置了数据源。
在确实加载的视图中,连同我设置的选择器的代表,我有:
switch firstValuePulled
case "Animals":
var pickerNumber1 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]
case "Sports":
var pickerNumber1 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]
default:
var pickerNumber1 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]
switch second ValuePulled
case "Animals":
var pickerNumber2 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]
case "Sports":
var pickerNumber2 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]
default:
var pickerNumber2 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]
switch thirdValuePulled
case "Animals":
var pickerNumber1 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]
case "Sports":
var pickerNumber1 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]
default:
var pickerNumber1 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]
对于选择器委托,错误显示在“使用未解析的标识符'pickerNumber1'”,“使用未解析的标识符'pickerNumber2'”等下方。可能是switch语句没有正确执行,因此委托没有值来显示选择器?
func numberOfComponents(in pickerView: UIPickerView) -> Int
return 2
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
if (pickerView.tag == 1)
return pickerNumber1[component].count;
else if (pickerView.tag == 2)
return pickerNumber2[component].count;
else
return pickerNumber3[component].count;
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String!
if (pickerView.tag == 1)
return pickerNumber1[component][row]
else if (pickerView.tag == 2)
return pickerNumber2[component][row]
else
return pickerNumber3[component][row]
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
if (pickerView.tag == 1)
eventOneValue = pickerNumber1[0][pickerView.selectedRow(inComponent: 0)]
eventOneNumber = pickerView.selectedRow(inComponent: 1) + 1
print(eventOneValue)
else if (pickerView.tag == 2)
eventTwoValue = pickerNumber2[0][pickerView.selectedRow(inComponent: 0)]
eventTwoNumber = pickerView.selectedRow(inComponent: 1) + 1
print(eventTwoValue)
else
eventThreeValue = pickerNumber3[0][pickerView.selectedRow(inComponent: 0)]
eventThreeNumber = pickerView.selectedRow(inComponent: 1) + 1
print(eventThreeValue)
【问题讨论】:
【参考方案1】:这是因为你创建了 在 switch 语句范围内本地的 pickerNumber1、pickerNumber2 和 U 在 picker 委托中使用! 你必须在类中创建它们并像这样在 Switch 中设置它的值
类 YourClassName
var pickerNumber1:Array = []
var pickerNumber2:Array = []
然后Switch语句将其用作
切换 firstValuePulled 案例“动物”: pickerNumber1 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]
case "Sports":
pickerNumber1 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]
default:
pickerNumber1 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]
切换第二个 ValuePulled 案例“动物”: pickerNumber2 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]
case "Sports":
pickerNumber2 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]
default:
pickerNumber2 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]
切换第三个ValuePulled 案例“动物”: pickerNumber1 = [["Cat", "Dog", "Bird", Fish],["1","2","3","4","5","6"]]
case "Sports":
pickerNumber1 = [["Basketball", "Hockey", "Baseball", "Tennis", "Football"],["1","2","3","4","5","6"]]
default:
pickerNumber1 = [["Apple", "Orange", "Peach"],["1","2","3","4","5","6"]]
然后在选择器代表中使用它们!
【讨论】:
我将 pickerNumber1、pickerNumber2 和 pickerNumber3 初始化为您提到的全局范围,但是我收到一个错误:“空集合文字需要显式类型”。你知道是什么问题吗? 你必须用它的包含类型初始化数组当你展示你的代码它包含字符串数组的数组所以你可以这样做 var pickerNumber1: Array> = []跨度> 【参考方案2】:在类块中声明这些选择器。
但更系统的方法是把它们做成出口,然后对那些拣货出口进行测试。
【讨论】:
以上是关于Swift 3.0 多个选择器视图无法识别数据源变量的主要内容,如果未能解决你的问题,请参考以下文章