我的 Swift 代码中有一个 int 的选择器视图。它在一个名为 numbers 的数组中有数字 1-7。当我要求numbers[row]
在函数内部打印时didSelectRow
,它会打印正确的数字。但在我的提交点击函数中,它只打印 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()
})
有谁知道为什么它在提交点击函数中总是返回零?