gpt4 book ai didi

ios - 在 swift 中将 UIPickerView 设置为 UITextField 的输入时出错

转载 作者:行者123 更新时间:2023-11-28 06:53:17 25 4
gpt4 key购买 nike

我是 swift 编程的新手,我想将 UIPickerView 设置为 UITextField 的输入。这是我的代码:

import UIKit

class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {

@IBOutlet var selectTransportCompany: UITextField!
var data = ["DHL Express", "Fed Ex", "TNT", "Express Mail"]
var picker = UIPickerView()

override func viewDidLoad() {
super.viewDidLoad()
picker.delegate = self
picker.dataSource = self
selectTransportCompany.inputView = picker
}

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

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

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
selectTransportCompany.text = data[row]

}

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


}

但是,当我运行这段代码时,我得到一个错误提示

Thread 1 : EXC_BAD_INSTRUCTION

进入调试器,我得到这个:

fatal error: unexpectedly found nil while unwrapping an Optional value

谁能知道是什么问题,我关注了this tutorial也是。

最佳答案

您只需将您的 View Controller 文本字段导出连接到您的 Storyboard selectTransportCompany 文本字段。

enter image description here

关于ios - 在 swift 中将 UIPickerView 设置为 UITextField 的输入时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34367864/

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