【发布时间】:2018-08-18 22:54:46
【问题描述】:
我正在尝试通过按下主视图控制器中的按钮将 JSON 值传输到另一个视图控制器中的标签。我的第一个视图控制器称为“ViewController”,第二个称为“AdvancedViewController”。下面的代码显示了我如何获取 JSON 数据,它工作正常,在 MainViewController 中的标签中显示 JSON 值,但是当我将 JSON 值发送到 AdvancedViewController 中的标签时,我按下按钮,它会加载AdvancedViewController 但标签值没有改变?我已经在我的 AdvancedViewController 中分配了标签,但我不确定它为什么不起作用。我正在尝试将其转移到高级视图控制器中的值“avc.Label”
主标签代码显示了我如何让它在我的 MainViewController 中工作
代码如下:
我的主视图控制器:
guard let APIUrl = URL(string: "https://api.openweathermap.org/data/2.5/weather?q=" + text + "&appid=e7b2054dc37b1f464d912c00dd309595&units=Metric") else { return }
//API KEY
URLSession.shared.dataTask(with: APIUrl) { data, response, error in
guard let data = data else { return }
let decoder = JSONDecoder()
//Decoder
do {
let weatherData = try decoder.decode(MyWeather.self, from: data)
if (self.MainLabel != nil)
{
if let gmain = (weatherData.weather?.first?.main) { //using .first because Weather is stored in an array
print(gmain)
DispatchQueue.main.async {
self.MainLabel.text! = String (gmain)
}
}
}
let avc = AdvancedViewController(nibName: "AdvancedViewController", bundle: nil)
if (avc.Label != nil)
{
if let mTest = (weatherData.weather?.first?.myDescription) { //using .first because Weather is stored in an array
DispatchQueue.main.async {
avc.Label.text! = String (mTest)
}
}
}
【问题讨论】: