我有2个按钮button1和button2
当按下button1时,我必须获取url1,当按下button2时,在以下函数中获取url2

           func fetchXMLData() {
    XMLParserFactory.fetchData(url: "https://brwterrgn.ergwgw.com/etwtt.cms") { (listOfXMLVM, error) in

        print("Fetch xml data")
        if error == nil {
            self.listOfNewsVM = listOfXMLVM!
            self.tableView.reloadData()
        }
        else {
            print(error?.localizedDescription ?? "Error")
        }
    }
}
}

最佳答案

向此fetchXMLData函数添加参数

func fetchXMLData(withUrl url: String)


还要用此url变量替换您的url

XMLParserFactory.fetchData(url: url)


现在,在每个IBAction调用中,此函数都将不同的url作为此方法的参数传递

@IBAction func button1pressed(_ sender: UIButton) {
    fetchXMLData(withUrl: "url1.com")
}

@IBAction func button2pressed(_ sender: UIButton) {
    fetchXMLData(withUrl: "url2.com")
}

关于swift - 快速按下按钮时如何更改URL,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53350574/

10-10 07:28