问题描述
例如,我在PickerView中选择(国家:苏格兰,城市:阿伯丁),然后在选择器视图中选择了这两个选项,就可以选择一个按钮,然后执行该操作,将我带到包含苏格兰,阿伯丁信息的另一页
我想做的是让我的PickerView选项将我带到另一个页面,其中包含选择了这些选项的特定数据。到目前为止,这就是我编写的代码。
import UIKit
class GrapplingViewController: UIViewController {
@IBOutlet weak var trainingTextField: UITextField!
@IBOutlet weak var areaTextField: UITextField!
@IBOutlet weak var focusTextField: UITextField!
let training = ["Skill Reciting","Sparring"]
let area = ["Takedowns","Top Battle","Bottom Battle","Survival"]
let focus = ["Full Training","Specific Training"]
var trainingPickerView = UIPickerView ()
var areaPickerView = UIPickerView ()
var focusPickerView = UIPickerView()
override func viewDidLoad() {
super.viewDidLoad()
title = "Grappling"
trainingTextField.inputView = trainingPickerView
areaTextField.inputView = areaPickerView
focusTextField.inputView = focusPickerView
trainingTextField.placeholder = "Select training type"
areaTextField.placeholder = "Select start position"
focusTextField.placeholder = "Select your focus"
trainingTextField.textAlignment = .center
areaTextField.textAlignment = .center
focusTextField.textAlignment = .center
trainingPickerView.delegate = self
trainingPickerView.dataSource = self
areaPickerView.delegate = self
areaPickerView.dataSource = self
focusPickerView.delegate = self
focusPickerView.dataSource = self
trainingPickerView.tag = 1
areaPickerView.tag = 2
focusPickerView.tag = 3
}
}
extension GrapplingViewController: UIPickerViewDataSource,UIPickerViewDelegate {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView,numberOfRowsInComponent component: Int) -> Int {
switch pickerView.tag {
case 1:
return training.count
case 2:
return area.count
case 3:
return focus.count
default:
return 1
}
}
func pickerView(_ pickerView: UIPickerView,titleForRow row: Int,forComponent component: Int) -> String? {
switch pickerView.tag {
case 1:
return training[row]
case 2:
return area[row]
case 3:
return focus[row]
default:
return "Data not found"
}
}
func pickerView(_ pickerView: UIPickerView,didSelectRow row: Int,inComponent component: Int) {
switch pickerView.tag {
case 1:
trainingTextField.text = training[row]
trainingTextField.resignFirstResponder()
case 2:
areaTextField.text = area[row]
areaTextField.resignFirstResponder()
case 3:
focusTextField.text = focus[row]
focusTextField.resignFirstResponder()
default:
return
}
}
@IBAction func startButton(_ sender: UIButton) {
}
}
解决方法
假设仅在选择器视图中选择了所有三个选项后,才想转到下一个视图控制器,您可以尝试一下。
每次使用didSelectRow
方法时,请保存所选的选项。例如:我将拥有三个属性,每个属性分别用于selectedTraining
,selectedArea
和selectedFocus
。
每次调用didSelectRow
时,在case语句中,检查其他两个变量是否具有值。如果是,则假设GrapplingViewController
已连接到另一个viewContoller,则可以调用performSegue
方法。