我的委托没有将我的 JSON 值传递给视图控制器中的 UILabel
My delegate isn't passing my JSON value to the UILabel in the View Controller
我创建了一个委托,将 JSON 值传递给我的视图控制器,这样我就可以更改两个 UILabel,但是当我 运行 我的应用程序时,标签不会更改。我已经坚持了很长时间,当我使用选择器视图更改货币时,我所做的一切只会导致应用程序保持不变。帮助将不胜感激。谢谢。
代表
protocol coinManagerDelegate {
func didUpdateCoin(price: String, currency: String)
func didFailWithError(error: Error)
}
函数
func performRequest(with Currency: String) {
let stringURL = baseURL + Currency + apiKey
AF.request(stringURL).responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
if let rate = json["rate"].double {
print(rate)
let totalRate = String(format: "%.2f", rate)
print(totalRate)
self.delegate?.didUpdateCoin(price: totalRate, currency: Currency)
}
case .failure(let error):
self.delegate?.didFailWithError(error: error)
}
}
}
视图控制器
extension ViewController: coinManagerDelegate {
func didFailWithError(error: Error) {
print(error)
}
func didUpdateCoin(price: String, currency: String) {
DispatchQueue.main.async {
self.bitcoinLabel.text = price
self.currencyLabel.text = currency
}
}
}
调用 performRequest
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selectedCurrency = coinManager.currencyArray[row]
coinManager.performRequest(with: selectedCurrency)
}
您必须在 ViewController
中设置 delegate
。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selectedCurrency = coinManager.currencyArray[row]
coinManager.delegate = self
coinManager.performRequest(with: selectedCurrency)
}
我创建了一个委托,将 JSON 值传递给我的视图控制器,这样我就可以更改两个 UILabel,但是当我 运行 我的应用程序时,标签不会更改。我已经坚持了很长时间,当我使用选择器视图更改货币时,我所做的一切只会导致应用程序保持不变。帮助将不胜感激。谢谢。
代表
protocol coinManagerDelegate {
func didUpdateCoin(price: String, currency: String)
func didFailWithError(error: Error)
}
函数
func performRequest(with Currency: String) {
let stringURL = baseURL + Currency + apiKey
AF.request(stringURL).responseJSON { response in
switch response.result {
case .success(let value):
let json = JSON(value)
if let rate = json["rate"].double {
print(rate)
let totalRate = String(format: "%.2f", rate)
print(totalRate)
self.delegate?.didUpdateCoin(price: totalRate, currency: Currency)
}
case .failure(let error):
self.delegate?.didFailWithError(error: error)
}
}
}
视图控制器
extension ViewController: coinManagerDelegate {
func didFailWithError(error: Error) {
print(error)
}
func didUpdateCoin(price: String, currency: String) {
DispatchQueue.main.async {
self.bitcoinLabel.text = price
self.currencyLabel.text = currency
}
}
}
调用 performRequest
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selectedCurrency = coinManager.currencyArray[row]
coinManager.performRequest(with: selectedCurrency)
}
您必须在 ViewController
中设置 delegate
。
func pickerView(_ pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
let selectedCurrency = coinManager.currencyArray[row]
coinManager.delegate = self
coinManager.performRequest(with: selectedCurrency)
}