点击按钮后,我正在尝试加载特定的网址。我在UIButton
操作中有webView代码,但我不知道如何将它拉入display / viewDidLoad方法中。但我需要能够使用不同的按钮更改URL。
import UIKit
class ViewController: UIViewController {
var radioURL = "http://www.ladiescanwetalk.org/category/podcasts/"
@IBOutlet weak var mainWebView: UIWebView!
@IBAction func loadRadioView(sender: UIButton) {
let getCurrentURL = NSURL (string: radioURL)
let requestCurrentURL = NSURLRequest(URL: getCurrentURL!)
self.mainWebView?.loadRequest(requestCurrentURL)
}
override func viewDidLoad() {
super.viewDidLoad()
}
答案 0 :(得分:0)
在我看来,实现您所要求的最佳方法是为每个值设置不同的tag
值(您可以使用Storyboard或直接通过代码执行此操作)。
您的代码将是这样的:
@IBAction func loadRadioView(sender: UIButton) {
if sender.tag == 1 {
radioURL = "http://anotherUrl"
} else if sender.tag == 2 {
radioURL = "http://yetanotherurl"
} else if sender.tag == 3 {
radioURL = "http://....."
}
let getCurrentURL = NSURL (string: radioURL)
let requestCurrentURL = NSURLRequest(URL: getCurrentURL!)
self.mainWebView?.loadRequest(requestCurrentURL)
}