gpt4 book ai didi

swift - PickerView 在文本字段输入中不显示任何内容

转载 作者:行者123 更新时间:2023-11-30 10:39:51 24 4
gpt4 key购买 nike

当我点击文本字段时,会出现输入区域,但只有 1 行,内容为空白。 (我期望 3 行:a、b 和 c)

我将 numberOfRowsInComponent 和 titleForRow 函数中的返回值更改为常量,但得到相同的结果(1 个空白行)。也许,这两个函数永远不会被调用。

请帮助我!

//In ViewDidLoad Function (ViewController.swift):
let myPickerView: MyPickerView = MyPickerView(arrData: ["a","b","c"]){ selected in
print(selected)
}

textField.inputView = myPickerView.picker





//In MyPickerView.swift
class MyPickerView: NSObject, UIPickerViewDelegate, UIPickerViewDataSource {

var arrData: Array<String>
var picker: UIPickerView
var action: (_ selected: String) -> Void

public init(
arrData: Array<String>,
action: @escaping (_ selected: String) -> Void
) {

self.arrData = arrData
self.action = action
self.picker = UIPickerView()

super.init()

self.picker.delegate = self
self.picker.dataSource = self
}

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

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

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

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
self.action(self.arrData[row])
}
}

最佳答案

我发现了问题。当我将 myPickerView 更改为全局变量(放在 ViewDidLoad 之外)时,一切都罚款了。

var myPickerView: MyPickerView?

//In ViewDidload
myPickerView = MyPickerView(arrData: ["a","b","c"]){ selected in
print(selected)
}

关于swift - PickerView 在文本字段输入中不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57089920/

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