Search code examples
iosarraysswiftxcodeuipickerview

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


For example I select in the PickerView ( country: Scotland, City: Aberdeen ) once I have selected both options in a picker view, I would select a button and have that action take me to another page with information about Scotland, Aberdeen specifically.

What I am trying to do is have my PickerView options take me to another page contained with data specific with those options selected. This is what I have coded so far.

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) {
              
               

}


}

Solution

  • Assuming you want to go the next view controller only after all three options are selected in the picker view you can try this.

    Each time your didSelectRow method, save the option that was selected. For eg: I would have three properties, one each for selectedTraining, selectedArea and selectedFocus.

    Each time the didSelectRow is called, in the case statement, check if the other two variables have a value. If yes, then, assuming the GrapplingViewController is hooked up to another viewContoller, you can calll performSegue method.