Picture of current version我目前有两个选择器视图。一个是国家选择器视图(美国和加拿大),另一个是州选择器视图。我想拥有它,以便在选择“美国”时显示美国各州,在选择“加拿大”时显示加拿大各省。这是到目前为止我所拥有的样本。帮助将不胜感激!
let countries = ["",
"USA",
"Canada",
]
let states = ["Alaska",
"Alabama",
"Arkansas"]
let provinces = ["Alberta",
"British Columbia",
"Manitoba",
"New Brunswick"]
//Sets picker view
let pickerView = UIPickerView()
//Holds current data in array
var currentArray: [String] = []
//Holds current text field
var activeTextField : UITextField!
var activeField: UITextField?
fileprivate func delegates() {
countryTextView.delegate = self
stateTextView.delegate = self
}
fileprivate func pickerViewDelegates() {
//Enables pickerview
pickerView.delegate = self
pickerView.dataSource = self
}
//Sets pickerview input
fileprivate func pickerViewSet() {
countryTextView.inputView = pickerView
stateTextView.inputView = pickerView
}
//Closes keyboard when done button is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
//Textfield delegate
func textFieldShouldBeginEditing(_ textField: UITextField) -> Bool {
activeTextField = textField
switch textField {
case countryTextView:
currentArray = countries
case stateTextView:
currentArray = states
default:
print("Default")
}
pickerView.reloadAllComponents()
return true
}
//Number of rows in picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
//One element of array per row
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return currentArray.count
}
//Puts the array element of whichever picker view is chosen into picker view
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
return currentArray[row]
}
//Selects the array element in the row thats chosen
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
print("Selected item is", currentArray[row])
activeTextField.text = currentArray[row]
}
我不说我的代码是“不错”。它只是按您想要的方式工作(据我了解您的意思)
如果从Blubb更改为USA,则第二个选择器将更新。
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
let countries = ["Blubb",
"USA",
"Canada",
]
let states = ["Alaska",
"Alabama",
"Arkansas"]
let statesCan = ["1", "2"]
//Sets picker view
let pickerView = UIPickerView()
let pickerView2 = UIPickerView()
//Holds current data in array
var currentArray: [String] = []
//Holds current text field
var activeTextField : UITextField!
var activeField: UITextField?
fileprivate func delegates() {
// countryTextView.delegate = self
// stateTextView.delegate = self
}
fileprivate func pickerViewDelegates() {
//Enables pickerview
pickerView.delegate = self
pickerView.dataSource = self
pickerView2.delegate = self
pickerView2.dataSource = self
}
//Sets pickerview input
fileprivate func pickerViewSet() {
// countryTextView.inputView = pickerView
// stateTextView.inputView = pickerView
}
//Closes keyboard when done button is pressed
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
//Number of rows in picker view
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
//One element of array per row
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView == self.pickerView2 {
return currentArray.count
} else {
return countries.count
}
}
//Puts the array element of whichever picker view is chosen into picker view
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
if pickerView == self.pickerView2 {
return currentArray[row]
} else {
return countries[row]
}
}
//Selects the array element in the row thats chosen
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == self.pickerView {
if row == 0 {
currentArray = states
} else {
currentArray = statesCan
}
pickerView2.reloadAllComponents()
}
// activeTextField.text = currentArray[row]
}
override func viewDidLoad() {
super.viewDidLoad()
currentArray = states
pickerViewDelegates()
self.view.addSubview(pickerView)
self.view.addSubview(pickerView2)
pickerView2.frame.origin.y = 400
}
}