我在ViewController
和Struct
中有两个pickerView,我在其中创建了一个函数,该函数将获取pickerViews的数据。在PickerView
的didSelectRow
函数中,我编写了以下代码。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == fromPickerViewRolled {
let optionArray1 = currencyManager.currencyArrayOption1[row]
}
else if pickerView == toPickerViewRolled {
let optionArray2 = currencyManager.currencyArrayOption2[row]
}
}
如何获取optionArray1
和…的数据。。数组2在if/else之外,这样我就可以在函数中使用。如有任何帮助,我们将不胜感激。期待着答案。
您只需要在if语句块之外声明一个数组。像这样:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var currencyArray: [YourType] = []
if pickerView == fromPickerViewRolled {
currencyArray = currencyManager.currencyArrayOption1[row]
}
else if pickerView == toPickerViewRolled {
currencyArray = currencyManager.currencyArrayOption2[row]
}
dump(currencyArray)
}