0

我正在尝试制作一个 iOS 应用程序,该应用程序允许用户从 UIPickerView 中选择一个州(美国),该 UIPickerView 中的每个值都分配有一个字符串(网站 url),在 UIPickerView 下方是一个启动 Web 的按钮位于提交按钮下的视图(我知道它已被弃用)。看起来像这样。我不想在 Web 视图中启动 google,而是想使用在单击提交按钮时选择的 UIPickerView 中提供给 State 的 URL 来启动网站。我的 WebController.swift 的代码如下。此图像显示了 iPhone (8) 的输出。有谁知道在这种情况下阅读的任何好的链接?任何想法也会很棒!

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
4

1 回答 1

1

你会得到这个(全局声明 var UIPickerView):

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

row = pickerView.selectedRow(inComponent: 0)
var urlSelected = states[row]["link"]
于 2018-03-27T04:59:44.577 回答