xcode11 如何在 PickerView 中给 selected 选项一个动作。这样当我 select selected 数组中的一个选项时,我可以对其执行操作

xcode11 how to give a selected option an action, in a PickerView. So that when I select an option in the selected array, I can perform an action to it

例如,我 select 在 PickerView(国家:苏格兰,城市:阿伯丁)中,一旦我在选择器视图中 select 编辑了两个选项,我就会 select 一个按钮并让该操作将我带到另一个页面,其中包含有关苏格兰,特别是阿伯丁的信息。

我想做的是让我的 PickerView 选项将我带到另一个页面,其中包含特定于这些选项的数据 selected。这是我到目前为止编写的代码。

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 方法时,保存所选的选项。例如:我将拥有三个属性,selectedTrainingselectedAreaselectedFocus.

各一个

每次调用didSelectRow时,在case语句中检查另外两个变量是否有值。如果是,那么假设 GrapplingViewController 连接到另一个 viewContoller,您可以调用 performSegue 方法。