Вы можете установить делегат pickerView и после нажатия viewController
Пример кода:
class ViewController: UIViewController {
@IBOutlet private weak var pickerView: UIPickerView!
struct PickerData {
let viewControllerId: String
let storyboardId: String
let title: String
}
private var pickersData: [PickerData] = []
override func viewDidLoad() {
super.viewDidLoad()
loadData()
pickerView.delegate = self
pickerView.dataSource = self
}
private func loadData() {
pickersData.append(
ViewController.PickerData(viewControllerId: "ViewController1", storyboardId: "Main", title: "Foo")
)
pickersData.append(
ViewController.PickerData(viewControllerId: "ViewController1", storyboardId: "Main", title: "Bar")
)
}
private func createViewController(from index: Int) -> UIViewController {
let model = pickersData[index]
let viewController = UIStoryboard(name: model.storyboardId, bundle: nil).instantiateViewController(withIdentifier: model.viewControllerId)
return viewController
}
private func pushViewController(viewController: UIViewController) {
// If Navigation
navigationController?.pushViewController(viewController, animated: true)
// If present
//present(viewController, animated: true, completion: nil)
}
}
extension ViewController: UIPickerViewDataSource {
func numberOfComponents(in pickerView: UIPickerView) -> Int {
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int {
return pickersData.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? {
return pickersData[row].title
}
}
extension ViewController: UIPickerViewDelegate {
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
pushViewController(viewController: createViewController(from: row))
}
}