gpt4 book ai didi

Swift 3.0 多个选择器 View 无法识别数据源变量

转载 作者:行者123 更新时间:2023-11-28 15:39:24 28 4
gpt4 key购买 nike

我的目标是在一个 viewController 上有 3 个 pickerView。每个 pickerView 将根据先前保存的设置列出不同的数据源,这些设置是从数据库中提取的。我已经根据使用如下所示的 switch 语句从数据库中提取的值设置了数据源。

在确实加载的 View 中,连同我设置的选择器的代表,我有:

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"]]

}

至于picker delegates,报错如下“Use of unresolved identifier 'pickerNumber1'”、“Use of unresolved identifier 'pickerNumber2'”等,难道是switch语句没有正确执行,因此delegates没有显示选择器的值?

 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)
}


}

最佳答案

这是因为你创造了pickerNumber1、pickerNumber2 在 switch 语句的范围内局部使用,而 U 在选择器委托(delegate)中使用!你必须在类里面创建它们并像那样在 Switch 中设置它的值

类你的类名{

var pickerNumber1:Array = []

var pickerNumber2:Array = []

然后 Switch 语句将其用作

switch firstValuePulled { 案例“动物”: pickerNumber1 = [["猫", "狗", "鸟", 鱼],["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 = [["猫", "狗", "鸟", 鱼],["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"]]

}

切换 thirdValuePulled { 案例“动物”: pickerNumber1 = [["猫", "狗", "鸟", 鱼],["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"]]

}

然后在选择器委托(delegate)中使用它们!

关于Swift 3.0 多个选择器 View 无法识别数据源变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43929278/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com