我有一个涉及 XCode 中选择器视图的问题。当我运行模拟并在选取器视图中选择一个选项时,文本会在两个文本框(我的代码中的 classOne 和 classThree)中弹出,控制所有具有选取器视图的(当前为 2 个,稍后将添加更多)。
我一直在尝试解决这个问题(具有不同步的单个选择器视图,并且可以在不同的文本框中放置不同的选择)但是即使我在函数中创建了两个单独的选择器视图和两个单独的案例,也没有任何效果。这是我的课程代码。
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)
}
据我所知,下面的代码是一个与 pickerViews 无关的按钮。您可能会在那里看到额外的长/不必要的代码,但工作正常并且对简化不感兴趣,因为它不会改变按钮的功能。
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"
}
}
}