自定义Picker视图无法将选中值填入TextField问题求助
Hey there! Let's get that selected Picker value showing up in your TextField. The main culprit here is usually not linking the Picker's selection events to your variables and then updating the TextField with those values. Here's a step-by-step fix tailored to your code:
1. Confirm Your Variable Setup
First, make sure your hours, halve, and type variables are ready to hold the selected values. Define them somewhere accessible (like in your ViewController):
var hours: String = "" var halve: String = "" var type: String = "" var timePickerData = [["4","5","6","7","8","9","10","11","12","13","14","15","16"],["0","15","30","45"],["Hours","Bins"]]
2. Implement the Picker's Selection Delegate Method
This is the critical part—you need to listen for when the user selects a row in the Picker, update your variables, and then refresh the TextField. If you're using UIPickerView, add this delegate method (make sure your ViewController conforms to UIPickerViewDelegate):
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) { // Update the corresponding variable based on which component was selected switch component { case 0: hours = timePickerData[0][row] case 1: halve = timePickerData[1][row] case 2: type = timePickerData[2][row] default: break } // Combine the values and set them to your TextField // Replace `yourTextField` with the actual name of your TextField instance yourTextField.text = "\(hours):\(halve) \(type)" }
3. Set Default Values on Load
Don't forget to initialize your variables and TextField with the Picker's default selected rows. Add this to viewDidLoad() (also ensure you've set the Picker's dataSource and delegate here):
override func viewDidLoad() { super.viewDidLoad() // Link the Picker to its data source and delegate timePicker.dataSource = self timePicker.delegate = self // Set initial values to the first row of each component hours = timePickerData[0][0] halve = timePickerData[1][0] type = timePickerData[2][0] // Populate the TextField with the default value yourTextField.text = "\(hours):\(halve) \(type)" }
4. Double-Check Your Data Source Methods
Make sure you've got the basic data source methods implemented to populate the Picker correctly (if you haven't already):
func numberOfComponents(in pickerView: UIPickerView) -> Int { return timePickerData.count } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return timePickerData[component].count } func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return timePickerData[component][row] }
Quick Note
If you're using SwiftUI instead of UIKit, the approach is a bit different—you'd bind your variables directly to a Picker with Picker(selection: $hours, ...) and then update the TextField via a computed property or directly in the view. But based on your mention of "3 components," it sounds like you're working with UIPickerView in UIKit, so the above steps should resolve your issue.
Give this a shot, and let me know if you hit any snags!
内容的提问来源于stack exchange,提问作者Stijnk008

