I have an issue involving picker views in XCode. When I run the simulation and choose an option in the picker view, the text pops up in both text boxes (classOne and classThree in my code) controlling all (2 currently, will add more later) which have a picker view.
I have been trying to solve this (having individual picker views that are not synchronized and can put different choices in different text boxes) but nothing is working even though I have made two separate picker views and two separate cases in the functions. Here is my code for the class.
import UIKit
class UnweightedGPACalculator: UIViewController, UIPickerViewDelegate, UITextFieldDelegate, UIPickerViewDataSource {
@IBOutlet weak var classOne: UITextField!
@IBOutlet weak var classTwo: UITextField!
@IBOutlet weak var classThree: UITextField!
@IBOutlet weak var classFour: UITextField!
@IBOutlet weak var classFive: UITextField!
@IBOutlet weak var classSix: UITextField!
@IBOutlet weak var classSeven: UITextField!
@IBOutlet weak var classEight: UITextField!
let vars = ["A", "B", "C", "D", "F"]
let vars2 = ["A", "B", "C", "D", "F"]
@IBOutlet weak var mainScreen: UITextView!
let currentText = UITextField()
var pickerView1 = UIPickerView()
var pickerView2 = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
pickerView1.dataSource = self
pickerView1.dataSource = self
pickerView2.delegate = self
pickerView2.dataSource = self
classOne.tag = 1
classThree.tag = 2
classOne.inputView = pickerView1
classThree.inputView = pickerView2
classOne.textAlignment = .center
classThree.textAlignment = .center
classOne.placeholder = "Class 1"
classThree.placeholder = "Class 2"
}
func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if pickerView.tag == classOne.tag{
return 5
}
else{
return 5
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
if pickerView.tag == classOne.tag{
return vars[row]
}
else{
return vars2[row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
classOne.text = vars[row]
classOne.resignFirstResponder()
classThree.text = vars2[row]
classThree.resignFirstResponder()
}
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
self.view.endEditing(true)
}
Code Below is for a button unrelated to the pickerViews as far as I know. You may see extra long/unnecessary code there but works fine and am not interested in simplifying as it does not change the function of the button.
override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?){
self.view.endEditing(true)
}
@IBAction func calculateButton(_ sender: Any) {
let c1 = classOne.text
let c2 = classTwo.text
let c3 = classThree.text
let c4 = classFour.text
let c5 = classFive.text
let c6 = classSix.text
let c7 = classSeven.text
let c8 = classEight.text
let group = [c1, c2, c3, c4, c5, c6, c7, c8]
var t2 : Double
var temp : Int = -1
var total = 0
var num = 0
for item in group {
if item == "A" || item == "A+" || item == "A-" || item == "A*"{
temp = 4
}
else if item == "B" || item == "B+" || item == "B-" || item == "B*"{
temp = 3
}
else if item == "C" || item == "C+" || item == "C-" || item == "C*"{
temp = 2
}
else if item == "D" || item == "D+" || item == "D-" || item == "D*"{
temp = 1
}
else if item == "F" || item == "F+" || item == "F-" || item == "F*" {
temp = 0
}
else if item == "" {
temp = -3
}
else {
temp = -2
}
if temp >= 0 {
total+=temp
num+=1
}
}
if temp >= 0 || temp == -3{
t2 = Double(total)/Double(num)
var doubleStr = String(format: "%.2f", ceil(t2*100)/100)
if (t2*100).remainder(dividingBy: 10) == 0{
doubleStr = String(format: "%.1f", ceil(t2*100)/100)
}
mainScreen.text = doubleStr
}
else{
mainScreen.text = "Error"
}
}
}