我有一個視圖控制器上的UI標籤和一個位於nsobject上的字符串(準確度)。我想把字符串放在不斷更新的uilabel上,因爲當你打印字符串(精度爲)時,它會更新。有人能幫我嗎 ?由於如何將UILabel連接到其他類
class Home: UIViewController {
@IBOutlet weak var lbl_accuracy: UILabel!
}
class PBLocation: NSObject, CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
if CLLocationManager.locationServicesEnabled() {
switch(CLLocationManager.authorizationStatus()) {
case .notDetermined, .restricted, .denied:
print("No access")
case .authorizedAlways, .authorizedWhenInUse:
let accuracy = String(format: "%.4f", (locations.last?.horizontalAccuracy)!)
print("accuracy = \(accuracy)")
}
} else {
print("Location services are not enabled")
}
}
}
使用您的viewController作爲CLLocationManagerDelegate那麼你可以直接改變值'self.lbl_accuracy.text = accuracy'在行,你正在打印'打印(「精度= \(精度)」)' –