制作UIButton单击“显示UIWebview”

时间:2016-06-09 20:26:28

标签: ios swift uiwebview uibutton

点击按钮后,我正在尝试加载特定的网址。我在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()
    }

1 个答案:

答案 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)

}