UIPickerView Swift

时间:2018-12-05 11:02:43

标签: ios swift xcode uipickerview

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

1 个答案:

答案 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}}中保存不同值的原因。