gpt4 book ai didi

ios - 选择器 View 数组行将只显示 0

转载 作者:行者123 更新时间:2023-11-28 15:19:30 26 4
gpt4 key购买 nike

我的 Swift 代码中有一个 int 的选择器 View 。它在一个名为 numbers 的数组中有数字 1-7。当我要求在 didSelectRow 函数中打印 numbers[row] 时,它会打印出正确的数字。但在我的提交点击函数中它只打印 0。它也在 viewWillAppear 中执行此操作。

我基本上希望该行是一个变量,我可以将其用作 int 来延迟某些代码。

let numbers = [1,2,3,4,5,6,7]
var days = Int()

override func viewDidLoad() {
super.viewDidLoad()

self.pickerView.delegate = self
self.pickerView.dataSource = self
}

override func viewWillAppear(_ animated: Bool) {
print(days)
}

func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return String(numbers[row])
}

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return numbers.count
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var days = numbers[row]
print(days)
}

@IBAction func submitTapped(_ sender: Any) {
databaseRef.child("numbers").child(self.TextField.text!).setValue(["thename" : UserDefaults.standard.value(forKey: "nametext")])
print(days)
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(days) , execute: {
databaseRef.child("numbers/\(self.TextField.text!)").removeValue()
})

有谁知道为什么它总是在 submit tapped 函数中返回零?

最佳答案

简单替换

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
var days = numbers[row]
print(days)

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
days = numbers[row]
print(days)

您的代码不起作用,因为您正在创建新变量并且没有引用您的实例变量。

关于ios - 选择器 View 数组行将只显示 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46267813/

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