Я работаю над UIPickerView. Мне нужно сделать кнопку зависимой от другой кнопки. Например, button3 будет зависеть от выбора button2, а button2 будет зависеть от выбора button1.
Я пробовал все «возможные» варианты, но не нашел результатов. Жду решения. Большое вам спасибо.
Вот мой код:
import UIKit
class ViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
//MARK:- IBOutlets
@IBOutlet weak var rslt1: UIButton!
@IBOutlet weak var rslt2: UIButton!
@IBOutlet weak var rslt3: UIButton!
@IBOutlet weak var rslt4: UIButton!
@IBOutlet weak var rslt5: UIButton!
@IBOutlet weak var rslt6: UIButton!
@IBOutlet weak var rslt7: UIButton!
@IBOutlet weak var button1: UIButton!
@IBOutlet weak var button2: UIButton!
@IBOutlet weak var button3: UIButton!
@IBOutlet weak var button4: UIButton!
@IBOutlet weak var button5: UIButton!
@IBOutlet weak var button6: UIButton!
@IBOutlet weak var button7: UIButton!
@IBOutlet weak var ImageView: UIImageView!
//MARK:- View Did Load
override func viewDidLoad() {
super.viewDidLoad()
}
//MARK:- Button Actions
@IBAction func button1Clicked(_ sender: Any) {
mainPicker.isHidden = false
mainPicker.delegate = self
mainPicker.dataSource = self
mainPicker.tag = 1
}
@IBAction func button2Clicked(_ sender: Any) {
mainPicker.isHidden = false
mainPicker.delegate = self
mainPicker.dataSource = self
mainPicker.tag = 2
}
@IBAction func buttonbutton3Clicked(_ sender: Any) {
mainPicker.isHidden = false
mainPicker.delegate = self
mainPicker.dataSource = self
mainPicker.tag = 3
}
@IBAction func button4Clicked(_ sender: Any) {
mainPicker.isHidden = false
mainPicker.delegate = self
mainPicker.dataSource = self
mainPicker.tag = 4
}
@IBAction func button5Clicked(_ sender: Any) {
mainPicker.isHidden = false
mainPicker.delegate = self
mainPicker.dataSource = self
mainPicker.tag = 5
}
@IBAction func button6Clicked(_ sender: Any) {
mainPicker.isHidden = false
mainPicker.delegate = self
mainPicker.dataSource = self
mainPicker.tag = 6
}
@IBAction func button7Clicked(_ sender: Any) {
mainPicker.isHidden = false
mainPicker.delegate = self
mainPicker.dataSource = self
mainPicker.tag = 7
}
let button1data = [“option 1”, "option 2”, "option 3”]
let button2data = [“A”: [“option 1”, "option 2”, "option 3”], “B”: [“option 1”, "option 2”, "option 3”], “C”: [“option 1”, "option 2”, "option 3”]]
//let button3data = [“option 1”, "option 2”, "option 3”]
let button4data = [“option 1”, "option 2”, "option 3”]
let button5data = [“option 1”, "option 2”, "option 3”]
let button6data = [“option 1”, "option 2”, "option 3”]
let button7data = [“option 1”, "option 2”, "option 3”]
//MARK:- Picker View Delegate and Datasources
func numberOfComponents(in pickerView: UIPickerView) -> Int{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
switch pickerView.tag {
case 1:
return button1data.count
case 2:
return button2data..count
case 3:
return button3data..count
case 4:
return button4data..count
case 5:
return button5data.count
case 6:
return button6data.count
case 7:
return button7data.count
default:
return 1
}
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
switch pickerView.tag {
case 1:
return button1data[row]
case 2:
return button2data[row]
case 3:
return button3data[row]
case 4:
return button4data[row]
case 5:
return button5data[row]
case 6:
return button6data[row]
case 7:
return button7data[row]
default:
return "Data not found"
}
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch pickerView.tag {
case 1:
rslt1.setTitle(button1data[row], for: .normal)
mainPicker.isHidden = true
case 2:
rslt2.setTitle(button2data[row], for: .normal)
mainPicker.isHidden = true
case 3:
rslt3.setTitle(button3data[row], for: .normal)
mainPicker.isHidden = true
case 4:
rslt4.setTitle(button4data[row], for: .normal)
mainPicker.isHidden = true
case 5:
rslt5.setTitle(button5data[row], for: .normal)
mainPicker.isHidden = true
case 6:
rslt6.setTitle(button6data[row], for: .normal)
mainPicker.isHidden = true
case 7:
rslt7.setTitle(button7data[row], for: .normal)
mainPicker.isHidden = true
default:
return
}
}
}