gpt4 book ai didi

ios - 在同一个 View Controller 上使用 Swift 中的 2 个 pickerviews 时,我只看到 ?-s,如何解决这个问题?

转载 作者:行者123 更新时间:2023-11-28 20:50:16 25 4
gpt4 key购买 nike

几个小时以来,我一直在努力解决这个问题,现在只剩下一个小问题了:

我在一个 View Controller 上使用了 2 个 UI 选择器,当我尝试选择一个(也就是我点击文本字段)时,我看到的只是问号,但如果我点击其中一个,文本就会出现在文本字段中。我只是看不到我在选择器中选择了什么。

我已经尝试过使用普通的选择器 View ,我为每个选择器使用了标签,但似乎没有任何效果。我知道它应该如何工作,我看了很多教程,但仍然缺少一些东西。你能帮我么?谢谢!

代码如下:

import UIKit

class SelectionViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}

@IBOutlet weak var ageTextField: UITextField!
@IBOutlet weak var weightTextField: UITextField!

@IBOutlet weak var genderTextField: UITextField!
@IBOutlet weak var lifeStyleTextField: UITextField!

let picker1 = UIPickerView()
let picker2 = UIPickerView()

var genders = ["Male", "Female"]
var lifeStyle = ["Sitting", "Normal", "Active"]

override func viewDidLoad() {
super.viewDidLoad()

picker1.dataSource = self
picker1.delegate = self

picker2.dataSource = self
picker2.delegate = self

genderTextField.inputView = picker1
lifeStyleTextField.inputView = picker2
}

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

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

func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
if pickerView == picker1 {
return genders.count
} else {
return lifeStyle.count
}
}

func pickerView(pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String! {

if pickerView == picker1 {
return genders[row]
} else if pickerView == picker2{
return lifeStyle[row]
}
else {
print("No pickerview selected.")
}
return ("Pickeview not selected")
}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == picker1 {
genderTextField.text = genders[row]
self.view.endEditing(false)
} else if pickerView == picker2{
lifeStyleTextField.text = lifeStyle[row]
self.view.endEditing(false)
}
else {
print("Love love love I want your love")
}
}
}

这是我看到的:

enter image description here

最佳答案

你需要这个签名(错过了_)

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

numberOfComponentsInPickerView 应该返回 1

关于ios - 在同一个 View Controller 上使用 Swift 中的 2 个 pickerviews 时,我只看到 ?-s,如何解决这个问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58992999/

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