gpt4 book ai didi

ios - 如何使用 UIPickerView 的结果分配字符串,使用字符串作为 WebView url

转载 作者:行者123 更新时间:2023-11-28 14:54:27 25 4
gpt4 key购买 nike

我正在尝试制作一个 iOS 应用程序,该应用程序允许用户从 UIPickerView 中选择一个州(美国),该 UIPickerView 具有分配给 UIPickerView 中每个值的字符串(网站 url),并且在 UIPickerView 下方是一个按钮启动位于提交按钮下方的 Web View (我知道它已被弃用)。 Looks like this .我不想在 Web View 中启动 google,而是想使用在单击提交按钮时选择的 UIPickerView 中提供给状态的 url 来启动网站。我的 WebController.swift 的代码如下。这 image显示到 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

最佳答案

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

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

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

关于ios - 如何使用 UIPickerView 的结果分配字符串,使用字符串作为 WebView url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503430/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com