gpt4 book ai didi

swift3 - UIPickerView 显示问号

转载 作者:行者123 更新时间:2023-12-05 08:42:30 28 4
gpt4 key购买 nike

我试图在 PickerView 中显示血型,但由于某些原因,选项仅显示为问号!

这是我的代码:

    class RegisterViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
//Methods for PickerViewDataSource

@available(iOS 2.0, *)
public func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickerDataSource.count;
}

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

@IBOutlet weak var selectedDate: UILabel!
@IBOutlet weak var birthDatePicker: UIDatePicker!

@IBOutlet weak var mobileNumberField: UITextField!
@IBOutlet weak var emailField: UITextField!
@IBOutlet weak var nameField: UITextField!

@IBOutlet weak var bloodTypePicker: UIPickerView!

@IBOutlet weak var selectedType: UILabel!

let pickerDataSource = ["Group A", "Group B", "Group AB", "Group O"];


override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
super.viewDidLoad()
self.bloodTypePicker.dataSource = self;
self.bloodTypePicker.delegate = self;

}

func pickerView(pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if(row == 0)
{
self.selectedType.text = "Group A"
}
else if(row == 1)
{
self.selectedType.text = "Group B"
}
else if(row == 2)
{
self.selectedType.text = "Group AB"
}
else
{
self.selectedType.text = "Group O"
}
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func datePickerAction(_ sender: Any) {

var dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
var strDate = dateFormatter.string(from: birthDatePicker.date)
self.selectedDate.text = strDate

}

}

这是显示内容的屏幕截图:

enter image description here

我正在学习本教程:http://sourcefreeze.com/ios-uipickerview-example-using-swift/

提前致谢。

最佳答案

你的函数 titleForRow 应该是这样的:

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?

你在 pickerView 之前忘记了一个 '_'

关于swift3 - UIPickerView 显示问号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41041692/

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