0

I would like to show a user different screens depending on what they choose in a UIPickerView. What would be the best way to implement this? Thanks!

import UIKit

class SelectKitViewController: UIViewController, UIPickerViewDelegate, UIPickerViewDataSource {


    @IBOutlet weak var kitPickerView: UIPickerView!

    let kits = ["Kit 1", "Kit 2"]
    var chosenKit: String?

    override func viewDidLoad() {
        super.viewDidLoad()

        kitPickerView.delegate = self
        kitPickerView.dataSource = self            
    }

    func numberOfComponents(in pickerView: UIPickerView) -> Int {
        return 1
    }

    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
        return kits[row]
    }

    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
        return kits.count
    }
    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        let value = kits[row]
        chosenKit = value
    }

    @IBAction func continuePressed(_ sender: Any) {
        performSegue(withIdentifier: "SelectReactionVolume", sender: nil)
    }
}

I would like to perform a segue to another View Controller when continue is pressed. Once the user chooses a kit they will have to enter additional info about the kit selected.

Rob
  • 415,655
  • 72
  • 787
  • 1,044
Hunter
  • 21
  • 7
  • Once the user chooses a kit they will have to enter additional info about the kit selected – Hunter Dec 19 '19 at 17:38
  • So, you’d generally just do the segue to your next view controller, but in this view controller’s [`prepare(for:sender:)`](https://developer.apple.com/documentation/uikit/uiviewcontroller/1621490-prepare), you’d just pass this `chosenKit`. See https://stackoverflow.com/a/29737851/1271826 or https://stackoverflow.com/a/31934786/1271826. – Rob Dec 19 '19 at 17:55
  • Thank you so much! – Hunter Dec 19 '19 at 18:08

0 Answers0