我一直在试图弄清楚为什么picker视图向我的函数发送冲突的结果。当我更改一个选择器视图时,它将覆盖由上一个选定选择器视图设置的值。pickerviews之间的共同点是它们是用相同的数组填充的。下面是控制pickerview和窗体屏幕截图的代码。当首先选择顶部贷方,最后选择底部贷方时,公式使用底部贷方进行计算。但是,如果我通过先选择最底层的贷方,然后再选择最上层的贷方来颠倒顺序,那么它将使用最上层的贷方来计算。选择包含60的值时也会发生同样的错误。选定的最后一个值设置传递给公式的值。每个选定值都需要保留其自己的选定值
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == currentLenderPicker
{
return newLender[row]
}
else if pickerView == newLenderPicker
{
return newLender[row]
}
else if pickerView == currentPaymentFrequencyPicker
{
return paymentFrequency[row]
}
else if pickerView == newPaymentFrequencyPicker
{
return paymentFrequency[row]
}
else if pickerView == mortgageTermPicker
{
return mortgageTerm[row]
}
else if pickerView == newMortgageTermPicker
{
return mortgageTerm[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == currentLenderPicker
{
return lenders.count
}
else if pickerView == newLenderPicker
{
return newLender.count
}
else if pickerView == currentPaymentFrequencyPicker
{
return paymentFrequency.count
}
else if pickerView == newPaymentFrequencyPicker
{
return paymentFrequency.count
}
else if pickerView == mortgageTermPicker
{
return mortgageTerm.count
}
else if pickerView == newMortgageTermPicker
{
return mortgageTerm.count
}
return 1
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == currentLenderPicker
{
orginalLender.text = lenders[row]
}
else if pickerView == newLenderPicker
{
newMortgageLender.text = newLender[row]
}
else if pickerView == currentPaymentFrequencyPicker
{
currentPaymentFrequency.text = paymentFrequency[row]
}
else if pickerView == newPaymentFrequencyPicker
{
newPaymentFrequency.text = paymentFrequency[row]
}
else if pickerView == mortgageTermPicker
{
currentMortgageTerm.text = mortgageTerm[row]
}
else if pickerView == newMortgageTermPicker
{
newMortgageTerm.text = mortgageTerm[row]
}
if let selectedLender = currentLender.filter({ $0.fiName == lenders[row] }).first {
Global.selectedRate = selectedLender
}
self.view.endEditing(false)
}
我用标签做了更改,但我仍然有同样的问题。更新了下面的代码。
// Load FI Information
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1
{
return newLender[row]
}
else if pickerView.tag == 11
{
return newLender[row]
}
else if pickerView.tag == 10
{
return paymentFrequency[row]
}
else if pickerView.tag == 15
{
return paymentFrequency[row]
}
else if pickerView.tag == 6
{
return mortgageTerm[row]
}
else if pickerView.tag == 17
{
return mortgageTerm[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 1
{
return lenders.count
}
else if pickerView.tag == 11
{
return newLender.count
}
else if pickerView.tag == 10
{
return paymentFrequency.count
}
else if pickerView.tag == 15
{
return paymentFrequency.count
}
else if pickerView.tag == 6
{
return mortgageTerm.count
}
else if pickerView.tag == 17
{
return mortgageTerm.count
}
return 1
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 1
{
orginalLender.text = lenders[row]
}
else if pickerView.tag == 11
{
newMortgageLender.text = newLender[row]
}
else if pickerView.tag == 10
{
currentPaymentFrequency.text = paymentFrequency[row]
}
else if pickerView.tag == 15
{
newPaymentFrequency.text = paymentFrequency[row]
}
else if pickerView.tag == 6
{
currentMortgageTerm.text = mortgageTerm[row]
}
else if pickerView.tag == 17
{
newMortgageTerm.text = mortgageTerm[row]
}
if let selectedLender = currentLender.filter({ $0.fiName == lenders[row] }).first {
Global.selectedRate = selectedLender
}
self.view.endEditing(false)
}
最佳答案
您应该使用标记来指示特定的pickerView。检查这个答案,它会把事情弄清楚的
Implement PickerView With Tag
希望有帮助。
关于swift - swift 冲突的UIPickerView,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/45426793/