iOS开发:如何将UIPickerView选中项的URL字符串传入WebView加载
Hey there, I've put together a straightforward implementation for your iOS app requirement. Let's break it down step by step to get everything working as you described:
1. Prepare State-URL Mapping
First, we need a way to link each U.S. state to its corresponding website URL. You can use an array of tuples to maintain the order of states in the picker, or a dictionary if order doesn't matter:
// Array of tuples (maintains picker order) let statesWithURLs: [(name: String, url: String)] = [ ("Alabama", "https://www.alabama.gov"), ("California", "https://www.ca.gov"), ("New York", "https://www.ny.gov"), ("Texas", "https://www.texas.gov"), // Add all other states and their URLs here ]
2. Set Up UIPickerView
Connect your UIPickerView, submit button, and UIWebView to your view controller via IBOutlets, then implement the picker's data source and delegate methods:
import UIKit class StateWebViewController: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate { @IBOutlet weak var statePicker: UIPickerView! @IBOutlet weak var loadWebsiteButton: UIButton! @IBOutlet weak var webView: UIWebView! // Note: UIWebView is deprecated let statesWithURLs: [(name: String, url: String)] = [ ("Alabama", "https://www.alabama.gov"), ("California", "https://www.ca.gov"), ("New York", "https://www.ny.gov"), ("Texas", "https://www.texas.gov") ] override func viewDidLoad() { super.viewDidLoad() // Link picker to data source/delegate statePicker.dataSource = self statePicker.delegate = self // Optional: Set a default selected state on load statePicker.selectRow(0, inComponent: 0, animated: false) } // MARK: UIPickerView DataSource func numberOfComponents(in pickerView: UIPickerView) -> Int { return 1 // Single column for state names } func pickerView(_ pickerView: UIPickerView, numberOfRowsInComponent component: Int) -> Int { return statesWithURLs.count } // MARK: UIPickerView Delegate func pickerView(_ pickerView: UIPickerView, titleForRow row: Int, forComponent component: Int) -> String? { return statesWithURLs[row].name } }
3. Implement Submit Button Action
Add an IBAction for your submit button to fetch the selected state's URL and load it into the web view:
@IBAction func loadWebsiteTapped(_ sender: UIButton) { // Get the selected row index let selectedRow = statePicker.selectedRow(inComponent: 0) let selectedURLString = statesWithURLs[selectedRow].url // Validate the URL before loading guard let targetURL = URL(string: selectedURLString) else { print("Invalid URL for selected state: \(selectedURLString)") // Optionally show an alert to the user here return } // Load the URL into the web view let urlRequest = URLRequest(url: targetURL) webView.loadRequest(urlRequest) }
A Quick Note About WebView
While you specified using the deprecated
UIWebView, I highly recommend switching toWKWebViewif possible. It's Apple's current, supported web view component with better performance, security, and modern web feature support. If you make the switch, the load code becomes simplywebView.load(urlRequest)(after replacingUIWebViewwithWKWebViewin your storyboard and code).
内容的提问来源于stack exchange,提问作者Duncan

