我有下面的代碼有問題:斯威夫特3 - UITextView的不更新
@IBOutlet var infoBox: UITextView!
LibPerso.startGPSLocationService(completion: {(response:Response, location:CLLocation?) -> Void in
if(response.hasError()){
//...
}
if let currentLocation = location, location != nil, location?.coordinate.latitude != 0, location?.coordinate.longitude != 0{
DispatchQueue.main.async {
print(self.infoBox.text) //#1
self.infoBox.text = "Lat : \(currentLocation.coordinate.latitude) \r\nLng: \(currentLocation.coordinate.longitude)"
print(self.infoBox.text) //#2
}
}
})
當我打開這個觀點,並呼籲在我的圖書館方法startGPSLocationService
(方法,它是一個服務來獲取GPS位置),每次我有回報時,我都成功更新了textView。
問題是,當我更改視圖(回到上一個視圖)並重新打開此視圖時,將調用所有代碼,但屏幕上未更新infoBox
。
我有打印兩個打印語句如下:
#1:緯度:48.7922072828896 LNG:10.16013687006524
#2:緯度:48.7922120134739 LNG:10.16015784907253
所以收件箱場正確更新,但在我的屏幕上,沒有任何反應。
我已經試過做self.infoBox.setNeedsLayout()
或setNeedsDisplay()
,但這也不會改變任何東西。
我認爲當我回去重新打開這個視圖時,對infoBox
的引用會丟失。
是從另一個屏幕返回時調用的完成塊。完成塊不是方法。 ? – Priyal
是的,顯示兩個「打印」,所以完成塊被稱爲 – Banana
infoBox的檢查框。 – KKRocks