Я хочу, чтобы button3 отображал элементы с помощью uipickerview на основе выбора button2. button3 будет зависеть от button2.
[«Круглый»: [«X», «Y», «Z»]]
[«Квадрат»: [«X», «Y» , «Z»], «Овал»: [«A», «B», «C»], «Прямоугольник»: [«1», «2», «3»]]
[«Вырезанный треугольник»: [«X», «Y», «Z»], «Сфера»: [«A», «B», «C»]]
Вот текущий xcode скрипт:
import UIKit
class ViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {
@IBOutlet weak var textField_0: UITextField!
@IBOutlet weak var textField_1: UITextField!
@IBOutlet weak var textField_2: UITextField!
@IBOutlet weak var btn1: UIButton!
@IBOutlet weak var btn2: UIButton!
@IBOutlet weak var btn3: UIButton!
@IBOutlet weak var mainPicker: UIPickerView!
private var shapeOptions = ["Option 1", "Option 2", "Option 3"]
private var shape1 = [["Round"],
["Square", "Oval", "Rectangle"],
["Triangle", "Sphere"]]
private var shape2 = [["X", "Y", "Z"],
["A", "B", "C"],
["1", "2", "3"]]
private var _currentSelection: Int = 0
var currentSelection: Int {
get {
return _currentSelection
}
set {
_currentSelection = newValue
mainPicker.reloadAllComponents()
textField_0.text = shapeOptions[_currentSelection]
textField_1.text = shape1[_currentSelection][0]
textField_2.text = shape2[_currentSelection][0]
}
}
override func viewDidLoad() {
super.viewDidLoad()
currentSelection = 0;
}
@IBAction func btn1Clicked(_ sender: Any) {
mainPicker.delegate = self
mainPicker.dataSource = self
mainPicker.tag = 0
mainPicker.isHidden = false
}
@IBAction func btn2Clicked(_ sender: Any) {
mainPicker.delegate = self
mainPicker.dataSource = self
mainPicker.tag = 1
mainPicker.isHidden = false
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
if pickerView.tag == 0 {
return shapeOptions.count
} else if pickerView.tag == 1 {
return shape1[currentSelection].count
} else {
return shape2[currentSelection].count
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
if pickerView.tag == 0 {
return shapeOptions[row]
} else if pickerView.tag == 1 {
return shape1[currentSelection][row]
} else {
return shape2[currentSelection][row]
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
currentSelection = row
textField_0.text = shapeOptions[row]
textField_0.resignFirstResponder()
} else if pickerView.tag == 1 {
textField_1.text = shape1[currentSelection][row]
textField_1.resignFirstResponder()
} else {
textField_2.text = shape2[currentSelection][row]
textField_2.resignFirstResponder()
}
}
}