我在一个视图控制器上有两个选择器视图(目前会有更多),当我在一个选择器视图中选择一个东西时,另一个选择器视图会自动更改为我在OTHER选择器视图中选择的值....如何使它们成为两种不同的选择器视图?我尝试为新的选择器视图创建新方法,但这似乎不起作用。我真的很感激任何帮助!提前致谢。
这是我的快捷代码:
import UIKit
class AddRatioViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
@IBAction func BackToHome(_ sender: Any) {
self.performSegue(withIdentifier: "BackToHome", sender: self)
}
@IBOutlet weak var ChooseDrivingGear: UIPickerView!
@IBOutlet weak var ChooseDrivingGear2: UIPickerView!
@IBOutlet weak var chooseBox1: UIButton!
@IBOutlet weak var chooseBox2: UIButton!
let drivingGear = ["1", "12", "36", "60", "84"]
override var shouldAutorotate: Bool {
return true
}
override func viewDidLoad() {
ChooseDrivingGear.isHidden = true
ChooseDrivingGear.delegate = self
ChooseDrivingGear.dataSource = self
ChooseDrivingGear2.isHidden = true
ChooseDrivingGear2.delegate = self
ChooseDrivingGear2.dataSource = self
super.viewDidLoad()
}
@IBAction func chooseBox1Pressed(_ sender: UIButton) {
sender.isHidden = true //added (to prevent overlap)
if ChooseDrivingGear.isHidden {
ChooseDrivingGear.isHidden = false
}
}
@IBAction func chooseBox2Pressed(_ sender: UIButton) {
sender.isHidden = true //added (to prevent overlap)
if ChooseDrivingGear2.isHidden {
ChooseDrivingGear2.isHidden = false
}
}
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return drivingGear[row]
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return drivingGear.count
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
chooseBox1.setTitle(drivingGear[row], for: .normal)
chooseBox2.setTitle(drivingGear[row], for: .normal)
ChooseDrivingGear.isHidden = true
chooseBox1.isHidden = false //added (to prevent overlap)
ChooseDrivingGear2.isHidden = true
chooseBox2.isHidden = false //added (to prevent overlap)
}
func textFieldShouldBeginEditing(textField: UITextField) -> Bool {
ChooseDrivingGear.isHidden = false
ChooseDrivingGear2.isHidden = false
return false
}
//second pickerView
}
答案 0 :(得分:0)
在委托方法中,检查pickerView的实例。您可以使用==运算符甚至切换大小写
来比较实例e.g:
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
switch pickerView {
case firstPickerView: //code here
case secondPickerView: //code here
default: break
}
}
确保为所有选择器视图创建插座,并且可以在ViewController中使用,如上所述,例如。 firstPickerView,secondPickerView等等
答案 1 :(得分:0)
他之所以发生这种情况,是因为您没有检查选择器视图委托方法中现在更改的选择器视图
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView == ChooseDrivingGear {
//do staf for first picker
} else {
//do staf for second one
}
}
或者最好使用标签
override func viewDidLoad() {
ChooseDrivingGear.isHidden = true
ChooseDrivingGear.delegate = self
ChooseDrivingGear.dataSource = self
ChooseDrivingGear.tag = 0
ChooseDrivingGear2.isHidden = true
ChooseDrivingGear2.delegate = self
ChooseDrivingGear2.dataSource = self
ChooseDrivingGear2.tag = 1
super.viewDidLoad()
}
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
if pickerView.tag == 0 {
//do staf for first picker
} else {
//do staf for second one
}
}
p.s同样在Swift中,变量应该以小写开头。