Duncan
Duncan

Reputation: 73

How to assign string with result from UIPickerView, use string as WebView url

I am trying to make an iOS app that allows a user to select a State (USA) from a UIPickerView that has a string (a website url) assigned to each value in the UIPickerView, and below the UIPickerView is a button that launches a Web View (I know it is deprecated) that is located under the submit button. Looks like this. Instead of launching google in the Web View, I want to launch the website using the url given to the State within the UIPickerView that is selected when the submit button is clicked. The code for my WebController.swift is below. This image shows the output to an iPhone (8). Does anyone know of any good links to read on this scenario? Any ideas would be awesome too!

import UIKit

class WebController: UIViewController, UIPickerViewDelegate, 
UIPickerViewDataSource {

    @IBOutlet weak var itemLabel: UILabel!

    @IBOutlet weak var urlLabel: UILabel!

    @IBOutlet weak var webView: UIWebView!

    var states = [
    ["title": "Alabama","link": "https://www.dmv.org/al-alabama/"],
    ["title": "Alaska", "link": ""],
    ["title": "Arizona", "link": ""],
    ["title": "Arkansas", "link": ""],
    ["title": "California", "link": ""],
    ["title": "Colorado", "link": ""],
    ["title": "Connecticut", "link": ""],
    ["title": "Deleware", "link": ""],
    ["title": "Florida", "link": ""],
    ["title": "Georgia", "link": ""],
    ["title": "Hawaii", "link": ""],
    ["title": "Idaho", "link": ""],
    ["title": "Illinois", "link": ""],
    ["title": "Indiana", "link": ""],
    ["title": "Iowa", "link": ""],
    ["title": "Kansas","link": ""],
    ["title": "Kentucky", "link": ""],
    ["title": "Louisiana", "link": ""],
    ["title": "Maine", "link": ""],
    ["title": "Maryland", "link": ""],
    ["title": "Massachusetts", "link": ""],
    ["title": "Michigan", "link": ""],
    ["title": "Minnesota", "link": ""],
    ["title": "Mississippi", "link": ""],
    ["title": "Missouri", "link": ""],
    ["title": "Montana", "link": ""],
    ["title": "Nebraska", "link": ""],
    ["title": "Nevada", "link": ""],
    ["title": "New Hampshire", "link": ""],
    ["title": "New Jersey", "link": ""],
    ["title": "New Mexico", "link": ""],
    ["title": "New York", "link": ""],
    ["title": "North Carolina", "link": ""],
    ["title": "North Dakota", "link": ""],
    ["title": "Ohio", "link": ""],
    ["title": "Oklahoma", "link": ""],
    ["title": "Oregon", "link": ""],
    ["title": "Pennsylvania", "link": ""],
    ["title": "Rhode Island", "link": ""],
    ["title": "South Carolina", "link": ""],
    ["title": "South Dakota", "link": ""],
    ["title": "Tennessee", "link": ""],
    ["title": "Texas", "link": ""],
    ["title": "Utah", "link": ""],
    ["title": "Vermont", "link": ""],
    ["title": "Virginia", "link": ""],
    ["title": "Washington", "link": ""],
    ["title": "West Virginia", "link": ""],
    ["title": "Wisconsin", "link": ""],
    ["title": "Wyoming", "link": ""]
    ]//end states[] array

    override func viewDidLoad() {
        super.viewDidLoad()
        itemLabel.text = states[0]["title"]
        urlLabel.text = states[0]["link"]
    }//end viewDidLoad()


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }//end didReceiveMemoryWarning()


    //How many parts to the wheel do you need?
    func numberOfComponents(in pickerView: UIPickerView) -> Int{
        return 1
    }//end numberOfComponents


    func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int{
        return states.count
    }//end numberOfRowsInComponent


    func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String?{
        return states[row]["title"]
    }//end titleForRow


    func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
        var itemSelected = states[row]["title"]
        var urlSelected = states[row]["link"]
        itemLabel.text = itemSelected
        urlLabel.text = urlSelected
    }//end didSelectRow


    @IBAction func submitStateBtn(_ sender: UIButton) {
        let url = URL(string: "http://www.google.com")//this line needs to change
        let requestObj = URLRequest(url: url!)
        webView.loadRequest(requestObj)
    }//end submitStateBtn

}//end WebController() class

Upvotes: 0

Views: 247

Answers (1)

Rahul_Chandnani
Rahul_Chandnani

Reputation: 275

You will get by this (declare var UIPickerView globally) :

var row = 0;
let pickerView :UIPickerView = UIPickerView()

row = pickerView.selectedRow(inComponent: 0)
var urlSelected = states[row]["link"]

Upvotes: 1

Related Questions