gpt4 book ai didi

ios - pickerView didSelectRow 正在输入错误的 UITextField

转载 作者:行者123 更新时间:2023-11-28 13:44:01 25 4
gpt4 key购买 nike

我正在尝试遵循以下 solution在一个 UIViewController 上拥有多个 UIPickerView。他们建议为我已经完成的每个 UITextfield 设置一个标签,并且能够实现行数、行标题和组件数。但是,我在实现 did select 行时遇到了绊脚石。当我选择 UITextField 时,它会突出显示另一个 UITextfield。下面是我到目前为止的代码。 类 DriverViewController: UIViewController {

var selectedTrack: String?

var firstDriver: String?

var secondDriver: String?

var thirdDriver: String?
let tracks = ["Melbourne", "Manama", "Shanghai", "Baku",
"Barcelona", "Monaco", "Montreal","Le Castellet","Spielberg",
"Silverstone","Hockenheim","Budapest","Francorchamps","Monza","Singapore","Sochi","Suzuka","Austin","Interlagos","Abu Dhabi"]
let drivers = ["Lewis Hamilton","Antonio Giovinazzi","Kimi Raikkonen","Charles Leclerc","Sebastian Vettel","Romain Grosjean","Kevin Magnussen","Lando Norris",
"Carlos Sainz","Valtteri Bottas","Sergio Perez","Lance Stroll","Pierre Gasly","Max Verstappen","Nico Hulkenberg","Daniel Ricciardo","Alexander Albon","Daniil Kvyat","Robert Kubica","George Russell"]
@IBOutlet weak var TrackTextField: UITextField!


@IBOutlet weak var firstTextField: UITextField!

@IBOutlet weak var secondTextField: UITextField!


@IBOutlet weak var thirdTextField: UITextField!


override func viewDidLoad() {
super.viewDidLoad()


createTrackPicker()
createDriverPicker()
createToolBar()
// Do any additional setup after loading the view.
}

func createTrackPicker() {
let trackPicker = UIPickerView()
trackPicker.tag = 0
trackPicker.delegate = self
TrackTextField.inputView = trackPicker
}

func createDriverPicker() {
let driverPicker = UIPickerView()
driverPicker.tag = 1
driverPicker.tag = 2
driverPicker.tag = 3
driverPicker.delegate = self
firstTextField.inputView = driverPicker
secondTextField.inputView = driverPicker
thirdTextField.inputView = driverPicker
}

func createToolBar() {

let toolBar = UIToolbar()
toolBar.sizeToFit()
let doneButton = UIBarButtonItem(title: "Done", style: .plain, target: self, action: #selector(ViewController.dismissKeyboard))
toolBar.setItems([doneButton], animated: false)
toolBar.isUserInteractionEnabled = true
TrackTextField.inputAccessoryView = toolBar
firstTextField.inputAccessoryView = toolBar
secondTextField.inputAccessoryView = toolBar
thirdTextField.inputAccessoryView = toolBar

}
@objc func dismissKeyboard() {
view.endEditing(true)
}
/*
// MARK: - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
// Get the new view controller using segue.destination.
// Pass the selected object to the new view controller.
}
*/

}

extension DriverViewController: UIPickerViewDelegate, UIPickerViewDataSource {
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 0 {
return tracks.count
} else {
return drivers.count
}
}


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

func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 0 {
return "\(tracks[row])"
} else {
return "\(drivers[row])"
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
selectedTrack = tracks[row]
TrackTextField.text = selectedTrack
} else if pickerView.tag == 1 {
firstDriver = drivers[row]
firstTextField.text = firstDriver
} else if pickerView.tag == 2 {
secondDriver = drivers[row]
secondTextField.text = secondDriver
} else if pickerView.tag == 3 {
thirdDriver = drivers[row]
thirdTextField.text = thirdDriver
}
}
}

这是它在屏幕上的样子。 Simulator Screenshot

最佳答案

看看你的函数:

func createDriverPicker() {
let driverPicker = UIPickerView()
driverPicker.tag = 1
driverPicker.tag = 2
driverPicker.tag = 3
driverPicker.delegate = self
firstTextField.inputView = driverPicker
secondTextField.inputView = driverPicker
thirdTextField.inputView = driverPicker
}

考虑运行此代码后 tag 属性的值,记住它只能有一个值。

您要么需要在每次文本字段开始编辑时更新选择器 View 的 tag 属性,要么您应该放弃使用标签并简单地检查当前哪个文本字段是第一响应者。参见 UIPickerView for each text field with different arrays (Swift/Firebase)有关如何完成的示例。

关于ios - pickerView didSelectRow 正在输入错误的 UITextField,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55669571/

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