我正在尝试将UIPickerView
与数据1到10一起使用。单击button "Extend"
时,我正在打印选择器中所选项目的值。有时它选择正确的值,但有时却没有。例如:如果我选择8有时会打印6。在逻辑上我做错了吗?
import UIKit
class ProfileViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
globalVariablePicker.picked = pickerData[row]
return pickerData[row]
}
struct globalVariablePicker {
static var picked = String()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
@IBOutlet weak var ProfileLabel1: UILabel!
@IBOutlet weak var ProfileLabel2: UILabel!
@IBOutlet weak var picker: UIPickerView!
@IBAction func Extend(_ sender: Any) {
print(globalVariablePicker.picked)
}
var pickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
pickerData = ["1","2","3","4","5","6","7","8","9","10"]
self.picker.delegate = self
self.picker.dataSource = self
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
ProfileLabel1.text = DetailStudentSponsoredViewController.globalVariableProfile.StringID
ProfileLabel2.text = DetailStudentSponsoredViewController.globalVariableProfile.StringName
}
}
答案 0 :(得分:0)
在方法中设置globalVariablePicker
,但在pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
代码:
import UIKit
class ProfileViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickerData[row]
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
globalVariablePicker.picked = pickerData[row]
}
struct globalVariablePicker {
static var picked = String()
}
override var preferredStatusBarStyle: UIStatusBarStyle {
return .lightContent
}
@IBOutlet weak var ProfileLabel1: UILabel!
@IBOutlet weak var ProfileLabel2: UILabel!
@IBOutlet weak var picker: UIPickerView!
@IBAction func Extend(_ sender: Any) {
print(globalVariablePicker.picked)
}
var pickerData: [String] = [String]()
override func viewDidLoad() {
super.viewDidLoad()
pickerData = ["1","2","3","4","5","6","7","8","9","10"]
self.picker.delegate = self
self.picker.dataSource = self
//Initial setup
globalVariablePicker.picked = pickerData[0] //Newline
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
ProfileLabel1.text = DetailStudentSponsoredViewController.globalVariableProfile.StringID
ProfileLabel2.text = DetailStudentSponsoredViewController.globalVariableProfile.StringName
}
}
注意:
每当UIPickerView
的行从可重用池中出队时,其中的值将被复制到globalVariablePicker
。这就是在globalVariablePicker
的{{1}}中保存不同值的原因。