gpt4 book ai didi

ios - 如何多次使用 UIPickerView 和 TextField ?

转载 作者:行者123 更新时间:2023-11-30 11:36:46 25 4
gpt4 key购买 nike

下面是我的代码,这允许一个 Textfield 有一个弹出的 pickerView 作为输入源。我想知道我需要添加什么才能允许第二个 TextField 也有一个弹出的 pickerView 作为输入源。

我已设法使firstName文本字段正常工作,但未能使secondNames正常工作。

class RefereeViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource, UITextFieldDelegate, UINavigationControllerDelegate
{

@IBOutlet weak var firstNameTextField: UITextField!

@IBOutlet weak var secondNameTextField: UITextField!


var firstNames = ["Lewis", "Jason","Alex","Mason"]

var secondNames = ["Davies", "Jones","Rees","Kristensen"]


var pickerView = UIPickerView()

override func viewDidLoad()

{

super.viewDidLoad()

pickerView.delegate = self

pickerView.dataSource = self

firstNameTextField.inputView = pickerView
firstNameTextField.textAlignment = .center
firstNameTextField.placeholder = "Select Name"
}

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

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{

return firstNames.count

}

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

return firstNames[row]

}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
firstNameTextField.text = reports[row]
}

最佳答案

class RefereeViewController: UIViewController, UIPickerViewDelegate,UIPickerViewDataSource, UITextFieldDelegate, UINavigationControllerDelegate
{

@IBOutlet weak var firstNameTextField: UITextField!

@IBOutlet weak var secondNameTextField: UITextField!


var firstNames = ["Lewis", "Jason","Alex","Mason"]

var secondNames = ["Davies", "Jones","Rees","Kristensen"]


let firstNamePicker = UIPickerView()
let secondNamePicker = UIPickerView()

override func viewDidLoad()

{

super.viewDidLoad()

firstNamePicker.delegate = self
firstNamePicker.dataSource = self
secondNamePicker.delegate = self
secondNamePicker.dataSource = self

firstNameTextField.inputView = firstNamePicker
firstNameTextField.textAlignment = .center
firstNameTextField.placeholder = "Select First Name"

secondNameTextField.inputView = secondNamePicker
secondNameTextField.textAlignment = .center
secondNameTextField.placeholder = "Select Second Name"

}

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

public func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{

return pickerView == firstNamePicker ? firstNames.count : secondNames.count

}

func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int)
{
if pickerView == firstNamePicker {
firstNameTextField.text = firstNames[row]
} else {
secondNameTextField.text = secondNames[row]
}
}

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

return pickerView == firstNamePicker ? firstNames[row] : secondNames[row]
}
}

关于ios - 如何多次使用 UIPickerView 和 TextField ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49676918/

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