How to change UIPickerViewData when I select the different UITextField? - swift

I want to show a UIPickerView when tapping a UITextField.
And I made a 'place' UITextField with a UIPickerView.
But, I don't know how to make another UITextField with a UIPickerView.
I tried to use 'switch', but UITextField was not a value, so I couldn't do it with 'switch'.
I want to change UIPickerViewData when I select the different UITextField.
How can I do?
Here's the codes.
Thank you!
#IBOutlet weak var place: UITextField!
#IBOutlet weak var product: UITextField!
#IBOutlet weak var number: UITextField!
let placeArray = ["A", "B", "C", "D", "E", "F"]
let productArray = ["Apple", "Banana", "Grape"]
let numberArray = ["1", "2", "3", "4", "5", "6"]
var pickerView = UIPickerView()
var textField: UITextField!
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return placeArray.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
place.text = placeArray[row]
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return placeArray[row]
}
override func viewDidLoad() {
super.viewDidLoad()
pickerView.delegate = self
pickerView.dataSource = self
let tapGesture = UITapGestureRecognizer(target: self, action: #selector(displayPickerView))
tapGesture.numberOfTapsRequired = 1
place.addGestureRecognizer(tapGesture)
}
#objc private func displayPickerView() {
if textField == nil {
self.textField = UITextField(frame: .zero)
textField.inputView = self.pickerView
self.view.addSubview(textField)
}
textField.becomeFirstResponder()
}

Very simple way to do this.
var placePickerView = UIPickerView()
var productPickerView = UIPickerView()
var numberPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
placePickerView.delegate = self
placePickerView.dataSource = self
place.inputView = placePickerView
productPickerView.delegate = self
productPickerView.dataSource = self
product.inputView = productPickerView
numberPickerView.delegate = self
numberPickerView.dataSource = self
number.inputView = numberPickerView
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var numberOfRows: Int = 1
if pickerView == placePickerView {
numberOfRows = placeArray.count
} else if pickerView == productPickerView {
numberOfRows = productArray.count
} else if pickerView == numberPickerView {
numberOfRows = numberArray.count
}
return numberOfRows
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == placePickerView {
place.text = placeArray[row]
} else if pickerView == productPickerView {
productType.text = productArray[row]
} else if pickerView == numberPickerView {
transactionType.text = numberArray[row]
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
var title: String?
if pickerView == placePickerView {
title = placeArray[row]
} else if pickerView == productPickerView {
title = productArray[row]
} else if pickerView == numberPickerView {
title = numberArray[row]
}
return title
}

Related

How to assign numbers for each UIpicker row

I am trying to develop a function in which the user enters a number in a TextField and based on the selection of the picker, it will calculate the number and display it either in a label or another text field. Now keep in mind, the picker has to retain String titles, but it will have different numbers for each row.
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate,
UIPickerViewDataSource {
#IBOutlet weak var numberInput: UITextField!
#IBOutlet weak var pickerControl: UIPickerView!
#IBOutlet weak var pickerControl2: UIPickerView!
#IBOutlet weak var resultField: UILabel!
var pickerData = ["Logistic","Administration","Clinic"]
var pickerData2 = ["Days","Weeks","Months"]
override func viewDidLoad() {
super.viewDidLoad()
pickerControl.delegate = self
pickerControl2.delegate = self
pickerControl.delegate = self
pickerControl2.delegate = self
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView,
numberOfRowsInComponent component: Int) -> Int {
if pickerView == pickerControl {
return pickerData.count
} else if pickerView == pickerControl2 {
return pickerData2.count
}
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row:
Int, forComponent component: Int) -> String? {
if pickerView == pickerControl {
return pickerData[row]
} else if pickerView == pickerControl2 {
return pickerData2[row]
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row:
Int, inComponent component: Int) -> String? {
let entry = Float(numberInput.text!) ?? 0.0
if pickerView == pickerControl {
return (Array(pickerData)[row])
}
if pickerView == pickerControl2 {
if Array(pickerData2)[row] == "Days" {
self.resultField.text = String(entry * 3)
}
if Array(pickerData2)[row] == "Weeks" {
self.resultField.text = String(entry * 4)
}
if Array(pickerData2)[row] == "Months" {
self.resultField.text = String(entry * 6)
}
return Array(pickerData2)[row]
}
return ""
}
}
The function signature for pickerView(didSelect...) isn't correct, so it's not getting called. You'll see that Xcode is giving you a warning about it. That function shouldn't be returning anything.
Here's an example of how to change it:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let entry = Float(numberInput.text ?? "") ?? 0.0
if pickerView == pickerControl {
return
}
switch pickerData2[row] {
case "Days":
self.resultField.text = String(entry * 3)
case "Weeks":
self.resultField.text = String(entry * 4)
case "Months":
self.resultField.text = String(entry * 6)
default:
break
}
}
You'll note that I also used a switch statement, which I think is a little cleaner looking. But, if you didn't and stuck to your if/else, you don't have to use Array() for everything -- you can do things like if pickerControl2[row] == "Days" { }
You could clean things up even further by defining pickerData2 as:
var pickerData2 : [(title: String, value: Float)] = [(title: "Days",value: 3),(title: "Weeks",value: 4),(title: "Months", value:6)]
Then, in titleForRow, you could return pickerData2[row].title
and later, in didSelectRow:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let entry = Float(numberInput.text ?? "") ?? 0.0
if pickerView == pickerControl {
return
}
self.resultField.text = String(entry * pickerData2[row].value)
}

multiple pickerview didSelectRow error in Swift

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

Two UIPickerViews in one ViewController

Two PickerViews selectTypeOfWorkChoices & selectLocationChoices do not appear correctly.
A function dismissPickerView() seems working well. However, another function "createPickerView()" has some problems. Although UIpickerviews appear, I cannot see the choices in UIPickerViews and I don't know why.
Could anyone help me figure out what's wrong with my code, please??
#IBOutlet weak var selectTypeOfWorkChoices: UIPickerView!
#IBOutlet weak var selectLocationChoices: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
createPickerView()
dismissPickerView()
}
var typeOfWork = ["--", "a", "b", "c"]
var location = ["--", "A", "B", "C"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var countrows : Int = typeOfWork.count
if pickerView == selectLocationChoices {
countrows = self.location.count
}
return countrows
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == selectTypeOfWorkChoices {
let titleRow = typeOfWork[row]
return titleRow
}
else if pickerView == selectLocationChoices {
let titleRow = location[row]
return titleRow
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == selectTypeOfWorkChoices {
selectedPriority = typeOfWork[row]
selectTypeOfWork.text = selectedPriority
self.selectTypeOfWork.text = self.typeOfWork[row]
}
else if pickerView == selectLocationChoices {
locationSelectedPriority = location[row]
selectLocation.text = locationSelectedPriority
self.selectLocation.text = self.location[row]
}
}
var selectedPriority : String?
var locationSelectedPriority : String?
func createPickerView() {
let pickerView = UIPickerView()
pickerView.delegate = self
self.selectTypeOfWorkChoices.delegate = self
self.selectTypeOfWorkChoices.dataSource = self
self.selectLocationChoices.delegate = self
self.selectLocationChoices.dataSource = self
selectTypeOfWork.inputView = selectTypeOfWorkChoices
selectLocation.inputView = selectLocationChoices
}
#objc func dismissPickerView() {
let toolBar = UIToolbar()
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title:"Done", style: .plain, target: self, action: #selector(self.dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
selectTypeOfWork.inputAccessoryView = toolBar
selectLocation.inputAccessoryView = toolBar
}
#objc func dismissKeyboard () {
view.endEditing(true)
}
Reload the picker view after assigning an input view to your text field.
func createPickerView() {
let pickerView = UIPickerView()
pickerView.delegate = self
self.selectTypeOfWorkChoices.delegate = self
self.selectTypeOfWorkChoices.dataSource = self
self.selectLocationChoices.delegate = self
self.selectLocationChoices.dataSource = self
selectTypeOfWork.inputView = selectTypeOfWorkChoices
selectLocation.inputView = selectLocationChoices
//Reload Pickerview
self.selectTypeOfWorkChoices.reloadAllComponents()
self.selectLocationChoices.reloadAllComponents()
}

How to get the value that the user chose from a UIPickerView?

This is what I'm trying to accomplish:
I want to take the value that the user selected from 2 different pickerViews and display that value in a textField
let drivingGear = ["1", "12", "36", "60", "84"]
let boxVal = chooseDrivingGear
let boxVal2 = chooseDrivenGear1
let newVal = boxVal / boxVal2
resultBox1.text = newVal
However I couldnt get that to work so I'm now trying this:
#IBAction func chooseBox1Pressed(_sender: UIButton) {
sender.isHidden = true
if chooseDrivingGear.isHidden {
chooseDrivingGear.isHidden = false
}
if chooseDrivingGear[row] == "1" {
let v1 = 1
}
but chooseDrivingGear[row] shows this error: use of unresolved identifier 'row'
(My other code for pickerview) UPDATE:
//first picker view
chooseDrivingGear.isHidden = true
chooseDrivingGear.delegate = self
chooseDrivingGear.dataSource = self
chooseDrivingGear.tag = 0
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
chooseBox1.setTitle(drivingGear[row], for: .normal)
chooseDrivingGear.isHidden = true
chooseBox1.isHidden = false
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return drivingGear[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return drivingGear.count
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
chooseDrivingGear.isHidden = false
chooseDrivingGear2.isHidden = false
chooseDrivingGear3.isHidden = false
chooseDrivenGear1.isHidden = false
chooseDrivenGear2.isHidden = false
chooseDrivenGear3.isHidden = false
return false
}
You can simply try getting the selectedRow from both the pickerViews, get the values for the selected rows and the perform calculation on the values as required to show in the textField.
I have provided a sample code for that below.
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate
{
#IBOutlet weak var pickerView1: UIPickerView!
#IBOutlet weak var pickerView2: UIPickerView!
#IBOutlet weak var textField: UITextField!
let drivingGear = ["1", "12", "36", "60", "84"]
override func viewDidLoad()
{
super.viewDidLoad()
}
#IBAction func showResult(_ sender: UIButton)
{
let val1 = self.drivingGear[self.pickerView1.selectedRow(inComponent: 0)]
let val2 = self.drivingGear[self.pickerView2.selectedRow(inComponent: 0)]
if let intVal1 = Int(val1), let intVal2 = Int(val2)
{
let result = intVal1 / intVal2
self.textField.text = String(result)
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return self.drivingGear.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return self.drivingGear[row]
}
}
Let me know if you still face any issues.

result from multiple UIPickers

I work in a project that has two pickers and one button.
When the user chooses from the first and second pickers
he click on the button .. and show the result in the textfield ..
for example:
if he chooses from the first picker "Jed" and from the second picker "Ahmed"..
now if he clicks the button the result will be in the textfield shown "OK" ..
this is what I did so far .. I don't know why its crashing when I click on the button ..
import UIKit
class ViewController: UIViewController , UIPickerViewDelegate , UIPickerViewDataSource {
#IBOutlet weak var theResult: UITextField!
#IBOutlet weak var cityTxt2: UITextField!
#IBOutlet weak var cityTxt: UITextField!
var city = ["Jed" , "Med" , "Ruh"]
var city2 = ["ahmed" , "mohammed" , "mustafa"]
let picker = UIPickerView()
let picker2 = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
picker2.delegate = self
picker2.dataSource = self
cityTxt.inputView = picker
cityTxt2.inputView = picker2
}
public func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}
public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if (pickerView == self.picker) {
return city.count
}
else if (pickerView == self.picker2) {
return city2.count
}
return city.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if (pickerView == self.picker) {
return city[row]
}
else if (pickerView == self.picker2) {
return city2[row]
}
return city[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if (pickerView == picker) {
self.cityTxt.text = self.city[row]
}
else if (pickerView == picker2) {
self.cityTxt2.text = self.city2[row]
}
self.view.endEditing(false)
}
#IBAction func getTheResult(_ sender: Any) {
let R1 = cityTxt.text
let R2 = cityTxt2.text
if (R1 == "Jed") && (R2 == "ahmed"){
theResult.text = "OK"
}else{
theResult.text = ""
}
}
}
I think the problem is in the Button
because when I click on it .. the program will crash ..