使用 UIPickerView 传递的数据不正确 Swift

Incorrect data passed with UIPickerView Swift

我正在尝试将 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
    }
}

在方法中设置 globalVariablePickerNOTpickerView(_ 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。这就是 pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?.

globalVariablePicker 保存不同值的原因