我在viewcontroller上有一个UI标签,在一个nsobject上有一个字符串(准确度)。我想把字符串放在不断更新的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")
}
}
}
答案 0 :(得分:1)
您可以使用delegate design pattern将位置准确度发送到Home VC。首先创建LocationUpdatable protocol
并添加updateLocationAccuracy(_ accuracy: String) function
。在PBLocation类中创建一个弱委托对象,现在您可以通过updateLocationAccuracy方法将精度对象发送到Home VC。最后确认Home VC中的LocationUpdatable协议并实现updateLocationAccuracy(_ accuracy: String)
函数,最重要的是将Home VC设置为LocationUpdatable
委托对象的委托。
protocol LocationUpdatable: class {
func updateLocationAccuracy(_ accuracy: String)
}
//Confirm to the LocationUpdatable
class Home: UIViewController, LocationUpdatable {
@IBOutlet weak var lbl_accuracy: UILabel!
let location = PBLocation()
var locationManager: CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
locationManager = CLLocationManager()
locationManager.delegate = location
locationManager.requestAlwaysAuthorization()
location.delegate = self
}
func updateLocationAccuracy(_ accuracy: String) {
lbl_accuracy.text = accuracy
}
}
//Create a delegate object
class PBLocation: NSObject, CLLocationManagerDelegate {
weak var delegate: LocationUpdatable?
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)")
if let delegate = delegate {
delegate.updateLocationAccuracy(accuracy)
}
}
} else {
print("Location services are not enabled")
}
}
}
答案 1 :(得分:0)
Introduction to Key-Value Observing Programming Guide
NSNotification& NSNotificationCenter
// new here
let accuracyNoti = NSNotification.Name(rawValue:"accuracyNoti")
// end
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)")
// new here
NotificationCenter.default.post(name: accuracyNoti, object: nil, userInfo: ["accuracy": accuracy)
// end
}
} else {
print("Location services are not enabled")
}
}
}
class Home: UIViewController {
@IBOutlet weak var lbl_accuracy: UILabel!
// new here
override func viewDidLoad() {
super.viewDidLoad()
NotificationCenter.default.addObserver(self, selector:#selector(didMsgRecv(notification:)),
name: accuracyNoti, object: nil)
}
deinit {
NotificationCenter.default.removeObserver(self)
}
func didMsgRecv(notification:NSNotification) {
let userInfo = notification.userInfo as! [String: AnyObject]
let accuracy = userInfo["accuracy"] as! String
lbl_accuracy.text = accuracy
}
// end
}