我正在尝试在两个不同的文本框上使用两个pickerviews(我将它们在文本框下方对齐并隐藏直到被调用)。我将隐藏pickerviews,直到准备使用并将两个文本框都绑定到它们对应的文本框。第一个pickerview工作正常,但是当我单击第二个pickerview时,我的列表没有出现,并且仍然隐藏。我已多次阅读代码,但找不到问题,并且观看了多个视频并检查了苹果文档,但无济于事。下面发布了代码以供查看和帮助。预先感谢!
class fifthViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource, UITextFieldDelegate {
@IBOutlet weak var heightInput: UITextField!
@IBOutlet weak var weightInput: UITextField!
@IBOutlet weak var bmiOutput: UITextField!
@IBOutlet weak var weightPicker: UIPickerView!
@IBOutlet weak var heightPicker: UIPickerView!
var height = ["48", "49", "50"]
var weight = ["90", "91", "92"]
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
var countrows : Int = height.count
if pickerView == weightPicker {
countrows = self.weight.count
}
return countrows
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView == heightPicker {
let titleRow = height[row]
return titleRow
}
else if pickerView == weightPicker {
let titleRow = weight[row]
return titleRow
}
return ""
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == heightPicker {
self.heightInput.text = self.height[row]
self.heightPicker.isHidden = true
}
else if pickerView == weightPicker {
self.weightInput.text = self.weight[row]
self.weightPicker.isHidden = true
}
}
func textFieldDidBeginEditing(_ textField: UITextField) {
if (textField == self.heightInput) {
self.heightPicker.isHidden = false
}
else if (textField == self.weightInput) {
self.weightInput.isHidden = false
}
}```
答案 0 :(得分:0)
在textFieldDidBeginEditing()
中,您需要将self.weightPicker.isHidden
设置为false
而不是self.weightInput.isHidden
。
func textFieldDidBeginEditing(_ textField: UITextField) {
if (textField == self.heightInput) {
self.heightPicker.isHidden = false
}
else if (textField == self.weightInput) {
self.weightPicker.isHidden = false
}
}
修改您的代码以使用inputView
s
我同意@ PaulW11的评论:
“如果您做出选择器视图,您几乎可以肯定会过得更好 每个文本字段的inputView。”
这是基本设置所需的更改。摆脱情节提要中的选择器,并断开@IBOutlet
连接。
然后添加以下修改:
// These are no longer outlets since they're created in code
var weightPicker: UIPickerView!
var heightPicker: UIPickerView!
override func viewDidLoad() {
super.viewDidLoad()
// set up the picker views
self.weightPicker = UIPickerView()
self.weightPicker.delegate = self
self.weightPicker.dataSource = self
self.heightPicker = UIPickerView()
self.heightPicker.delegate = self
self.heightPicker.dataSource = self
self.heightInput.inputView = self.heightPicker
self.weightInput.inputView = self.weightPicker
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == heightPicker {
self.heightInput.text = self.height[row]
// make picker go away after value has been selected
self.heightInput.resignFirstResponder()
}
else if pickerView == weightPicker {
self.weightInput.text = self.weight[row]
// make picker go away after value has been selected
self.weightInput.resignFirstResponder()
}
}