gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 07:34:23 25 4
gpt4 key购买 nike

我正在尝试将 UIPickerView 与数据 1 到 10 一起使用。单击 按钮“扩展” 时,我正在打印选择器所选项目的值.有时它会选择正确的值,但有时不会。例如:如果我有时选择 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
}
}

最佳答案

在方法中设置 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。这就是在 pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?globalVariablePicker 中保存不同值的原因。 p>

关于ios - 使用 UIPickerView Swift 传递的数据不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53630785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com