1. First of all, you need to create a dataSource
that you can use in UIPickerView
. It must contain a title
and link
for all the rows
that you want to show in the UIPickerView
, i.e.
let data = [
[
"title": "Kansas",
"link": "https://www.dmv.org/ks-kansas/"
],
[
"title": "Title-1",
"link": "Link-1"
],
[
"title": "Title-2",
"link": "Link-2"
],
[
"title": "Title-3",
"link": "Link-3"
]
]
2. Next, you need to implement UIPickerViewDataSource
and UIPickerViewDelegate
methods in your UIViewController
, i.e.
func numberOfComponents(in pickerView: UIPickerView) -> Int
{
return 1
}
func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int
{
return data.count
}
func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?
{
return data[row]["title"]
}
Don't forget to set the delegate
and dataSource
of UIPickerView
in viewDidLoad()
.
@IBOutlet weak var pickerView: UIPickerView!
override func viewDidLoad()
{
super.viewDidLoad()
}
3. Now, on UIButton
press, you need to get the selected row's
link
in UIPickerView
and send it to AnotherViewController
that contains a UIWebView
, i.e.
@IBAction func onTapDoneButton(_ sender: UIButton)
{
let anotherController = self.storyboard?.instantiateViewController(withIdentifier: "AnotherViewController") as! AnotherViewController
anotherController.urlString = self.data[self.pickerView.selectedRow(inComponent: 0)]["link"]
self.present(anotherController, animated: true, completion: nil)
}
4. Here is the code for AnotherViewController
that contains the UIWebView
.
class AnotherViewController: UIViewController
{
var urlString: String?
@IBOutlet weak var webView: UIWebView!
override func viewDidLoad()
{
super.viewDidLoad()
if let urlString = self.urlString, let url = URL(string: urlString)
{
self.webView.loadRequest(URLRequest(url: url))
}
}
}
Let me know if you still face any issues.