我需要每20秒更新一次UILabel
文本。 這裏一切正常,但我的UILabel
不會改變。爲什麼我的UILabel文本在Xcode 8.1中不會在Swift3中更改
數據從URL和短語也正確加載。當我嘗試 self.team1Score = "1"
在viewDidLoad
然後它的工作。
請看看我的代碼,並建議我如何每20秒更換UILabel
文字。
import UIKit
class WatchLiveVideo: UIViewController {
@IBOutlet var team1Score: UILabel!
@IBOutlet var team2Score: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
self.getScore()
//run getScore in every 20 Seconds
_ = Timer.scheduledTimer(timeInterval: 20, target: self, selector: #selector(WatchLiveVideo.self.getScore), userInfo: nil, repeats: true)
}
func getScore()
{
let myUrl = URL(string: "http://myurl.com");
var request = URLRequest(url:myUrl!)
request.httpMethod = "POST"// Compose a query string
//input data bind to send server
let streamID = getStreamID
let postString = "streamID=\(streamID)"
request.httpBody = postString.data(using: String.Encoding.utf8)
let task = URLSession.shared.dataTask(with: request) { (data: Data?, response: URLResponse?, error: Error?) in
if error != nil
{
print("error=\(error)")
return
}
do {
let json = try JSONSerialization.jsonObject(with: data!, options: .mutableContainers) as? NSDictionary
if let parseJSON = json {
let status = parseJSON["status"] as? String
let team1 = parseJSON["team1"] as? String
let team2 = parseJSON["team2"] as? String
if(status == "1")
{
self.team1Score.text = team1
self.team2Score.text = team2
}
else if(status == "0")
{
print("No Score Found")
}
}
} catch {
print(error.localizedDescription)
}
}
task.resume()
}
}
你試過在主隊列調度self.team1Score.text = TEAM1? – Adamsor
嗨Adamsor,謝謝你的回覆。是我嘗試與DispatchQueue.main.async(){ self.team1Score.text = self.team1 self.team2Score.text = self.team2 }在ViewDidLoad –
「當我嘗試self.team1Score =」1「在ViewDidLoad然後工作。「怎麼會這樣? –