无法将所选值从 UIPickerView 保存到数组
Not able to save selected value from a UIPickerView to an array
到目前为止我已经尝试使用
func pickerView1(_ helpTypePicker: UIPickerView, didSelectRow row: Int, inComponent component: Int {
infoListInit.requestInfoList[4] = (helpTypePickerValues[row])
}
但是这个值只是没有保存到数组中。我尝试在保存后打印该值,但无济于事。我的 viewDidLoad 函数中确实有 helpTypePicker 作为自我委托,所以我不知道从这里去哪里。 UITextFields 中的其他文本值保存良好,但我的 UIPickerViews none 在这方面工作正常
这里是完整的 class:
import Foundation
import UIKit
class requestHelpTypePickerScreen: UIViewController,
UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent
component: Int) -> Int {
if pickerView.tag == 1 {
return helpTypePickerValues.count
} else {
return languagePickerValues.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return "\(helpTypePickerValues[row])"
} else {
return "\(languagePickerValues[row])"
}
}
// Mark: outlets
@IBOutlet weak var helpTypePicker: UIPickerView!
@IBOutlet weak var languagePicker: UIPickerView!
@IBOutlet weak var helpTypePickerLabel: UILabel!
let helpTypePickerValues = ["Academic/Professional", "Donate", "Food/Groceries", "Medication", "Emotional Support", "Misc."]
let languagePickerValues = ["Arabic", "Chinese", "Spanish", "English", "French", "Hindi/Urdu", "Korean", "Russian"]
override func viewDidLoad() {
super.viewDidLoad()
helpTypePicker.delegate = self
languagePicker.delegate = self
self.helpTypePicker.selectRow(2, inComponent: 0, animated: true)
self.languagePicker.selectRow(3, inComponent: 0, animated: true)
}
// Mark: actions
@IBAction func buttonPressed(_ sender: Any) {
func pickerView(_ helpTypePicker: UIPickerView,
didSelectRow row: Int,
inComponent component: Int) {
infoListInit.requestInfoList[5] = (helpTypePickerValues[row])
}
// this is called pickerView1 because it would conflict with the above
// function if called pickerView
func pickerView1(_ languagePicker: UIPickerView,
didSelectRow row1: Int,
inComponent component: Int) {
infoListInit.requestInfoList[6] = (languagePickerValues[row1])
}
performSegue(withIdentifier: "pickersDone", sender: nil)
}
您的代码中有一个错误,Delegate 指示当在 UIPickerView
中编辑新行时 select,将调用函数 pickerView(_:didSelectRow:inComponent:)
(参数名称没关系)。
在您的情况下,当任何选择器 select 调用新行 pickerView(_:didSelectRow:inComponent:)
时,选择器作为第一个位置的参数传递。你应该检查哪个选择器被修改了,然后改变你的逻辑。
你的情况:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == helpTypePicker {
infoListInit.requestInfoList[5] = helpTypePickerValues[row]
} else {
infoListInit.requestInfoList[6] = languagePickerValues[row]
}
}
到目前为止我已经尝试使用
func pickerView1(_ helpTypePicker: UIPickerView, didSelectRow row: Int, inComponent component: Int {
infoListInit.requestInfoList[4] = (helpTypePickerValues[row])
}
但是这个值只是没有保存到数组中。我尝试在保存后打印该值,但无济于事。我的 viewDidLoad 函数中确实有 helpTypePicker 作为自我委托,所以我不知道从这里去哪里。 UITextFields 中的其他文本值保存良好,但我的 UIPickerViews none 在这方面工作正常
这里是完整的 class:
import Foundation
import UIKit
class requestHelpTypePickerScreen: UIViewController,
UIPickerViewDataSource, UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent
component: Int) -> Int {
if pickerView.tag == 1 {
return helpTypePickerValues.count
} else {
return languagePickerValues.count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 1 {
return "\(helpTypePickerValues[row])"
} else {
return "\(languagePickerValues[row])"
}
}
// Mark: outlets
@IBOutlet weak var helpTypePicker: UIPickerView!
@IBOutlet weak var languagePicker: UIPickerView!
@IBOutlet weak var helpTypePickerLabel: UILabel!
let helpTypePickerValues = ["Academic/Professional", "Donate", "Food/Groceries", "Medication", "Emotional Support", "Misc."]
let languagePickerValues = ["Arabic", "Chinese", "Spanish", "English", "French", "Hindi/Urdu", "Korean", "Russian"]
override func viewDidLoad() {
super.viewDidLoad()
helpTypePicker.delegate = self
languagePicker.delegate = self
self.helpTypePicker.selectRow(2, inComponent: 0, animated: true)
self.languagePicker.selectRow(3, inComponent: 0, animated: true)
}
// Mark: actions
@IBAction func buttonPressed(_ sender: Any) {
func pickerView(_ helpTypePicker: UIPickerView,
didSelectRow row: Int,
inComponent component: Int) {
infoListInit.requestInfoList[5] = (helpTypePickerValues[row])
}
// this is called pickerView1 because it would conflict with the above
// function if called pickerView
func pickerView1(_ languagePicker: UIPickerView,
didSelectRow row1: Int,
inComponent component: Int) {
infoListInit.requestInfoList[6] = (languagePickerValues[row1])
}
performSegue(withIdentifier: "pickersDone", sender: nil)
}
您的代码中有一个错误,Delegate 指示当在 UIPickerView
中编辑新行时 select,将调用函数 pickerView(_:didSelectRow:inComponent:)
(参数名称没关系)。
在您的情况下,当任何选择器 select 调用新行 pickerView(_:didSelectRow:inComponent:)
时,选择器作为第一个位置的参数传递。你应该检查哪个选择器被修改了,然后改变你的逻辑。
你的情况:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == helpTypePicker {
infoListInit.requestInfoList[5] = helpTypePickerValues[row]
} else {
infoListInit.requestInfoList[6] = languagePickerValues[row]
}
}