0

I have 2 pickerviews, 1 pickerview is to select a crypto-currency from an Array and the other pickerview is used to select a currency from an array. The problem I am having is when I run the app on simulator and select the cryptocurreny in cryptoPicker the app it also selects the same array value from currencyPicker and vice a versa. I don't want the array value of 0 to be pulled from both arrays unless it is selected by the user.

@IBOutlet weak var cryptoCurrentRate: UILabel!
@IBOutlet weak var currencyLabel: UILabel!
@IBOutlet weak var currencyPicker: UIPickerView!
@IBOutlet weak var cryptoPicker: UIPickerView!
@IBOutlet weak var cryptoLabel: UILabel!


var coinManager = CoinManager()


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view.
    coinManager.delegate = self
    currencyPicker.dataSource = self
    currencyPicker.delegate = self
    cryptoPicker.dataSource = self
    cryptoPicker.delegate = self   

}

}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
       return 1
   }


    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        if pickerView == currencyPicker {
        return coinManager.currencyArray.count
        }
        if pickerView == cryptoPicker {
        return coinManager.cryptoArray.count
    }
        return 0
    }
    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        if pickerView == currencyPicker {
        return coinManager.currencyArray[row]
        }
        if pickerView == cryptoPicker {
        return coinManager.cryptoArray[row]
    }
        return ""
    }

    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let selectedCurrency = coinManager.currencyArray[row]
        let selectedCrypto = coinManager.cryptoArray[row]
        coinManager.fetchCryptoCoin(assetIdBase: selectedCrypto, assetIdQuote: selectedCurrency)
    }

}

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
galvatron
  • 21
  • 3

1 Answers1

0

Do the same check for picker and in 'didSelectRow', you can do it like this:

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
     var selectedCurrency: CurrencyModel? = nil
     var selectedCrypt: CryptModel? = nil


    if pickerView == currencyPicker {
        selectedCurrency = coinManager.currencyArray[row]
    }
    if pickerView == cryptoPicker {
        selectedCrypto = coinManager.cryptoArray[row]
    }

    coinManager.fetchCryptoCoin(assetIdBase: selectedCrypt == nil ? coinManager.cryptoArray[.zero] : selectedCrypt!, assetIdQuote: selectedCurrency == nil ? coinManager.currencyArray[.zero] : selectedCurrency!)
}

By default it will get the first value from 'coinManager.currencyArray' or 'coinManager.cryptoArray'

Adriatik Gashi
  • 332
  • 4
  • 10
  • sorry for the late reply, I tried this but it still ended up doing the error of showing the same row on each picker. So if row 1 was selected on picker 1 it was also selected on picker 2. I ended up just putting both the selectedCurrency and selectedCrypt onto 1 picker (the same picker) because that method was easier to accomplish my goal. But I would like to know how to do this for a probable future project. – galvatron May 05 '20 at 05:46