1

I am pretty new to working with Swift. I am making an iPhone app using Swift that has a view with a label "Choose State" and a picker view that contains all 50 states in USA.

I am trying to assign each item in the picker view to a specified url that matches that state's DMV website address. (Example: user selects Kansas, when they hit 'done/submit' they are segued to another view controller that has a Webkit View that loads "https://www.dmv.org/ks-kansas/".

I have been searching and have not found anything online that helps.

halfer
  • 19,824
  • 17
  • 99
  • 186
Duncan
  • 73
  • 1
  • 8

3 Answers3

0

You should create a dictionary like this

 var dic = [String:String]()

key represents the state , and value the url , title of the picker is the keys of the dictionary and when you click get the url value

and here how to implement the picker: PickerView

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87
0
  • Get the US states as JSON from https://gist.github.com/mshafrir/2646763, the states_titlecase.json version.
  • Add a new key value pair in each dictionary for the last path component of the URL for example

    {
       "name": "Kansas",
       "abbreviation": "KS",
       "urlPath" : "ks-kansas/"
    }
    
  • Decode the JSON with JSONDecoder of Swift 4 into an array of struct.

  • Use the array as data source for the picker view.
  • Show name or abbreviation to the user.
  • When the user picks a value compose the URL with

    let url = URL(string: "https://www.dmv.org")!.appendingPathComponent(state.urlPath)
    
vadian
  • 274,689
  • 30
  • 353
  • 361
0

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.

PGDev
  • 23,751
  • 6
  • 34
  • 88