我有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")
}
}
}
}
答案 0 :(得分:0)
您可以尝试将url作为参数并将其从调用者位置更改
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
fetchXMLData(url: "https://value.com/etwtt.cms")
}
func buttonAct(_ sender: UIButton) {
fetchXMLData(url: "https://brwterrgn.ergwgw.com/etwtt.cms")
}
func fetchXMLData(url:String) {
XMLParserFactory.fetchData(url:url) { (listOfXMLVM, error) in
print("Fetch xml data")
if error == nil {
self.listOfNewsVM = listOfXMLVM!
self.tableView.reloadData()
}
else {
print(error?.localizedDescription ?? "Error")
}
}
}
修改
class FirstVC: UIViewController {
func showSecond(_ sender: UIButton) {
let sec = //
sec.delegate = self
present/push(sec)
}
func fetchXMLData(url:String) {
XMLParserFactory.fetchData(url:url) { (listOfXMLVM, error) in
print("Fetch xml data")
if error == nil {
self.listOfNewsVM = listOfXMLVM!
self.tableView.reloadData()
}
else {
print(error?.localizedDescription ?? "Error")
}
}
}
class SecondVC: UIViewController {
weak var delegate:FirstVC?
func buttonAct(_ sender: UIButton) {
delegate?.fetchXMLData(url: "https://brwterrgn.ergwgw.com/etwtt.cms")
}
}
答案 1 :(得分:0)
向此fetchXMLData函数添加参数
func fetchXMLData(withUrl url: String)
还用此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")
}