2017-06-03 76 views
0

我有一个视图控制器上的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") 
     } 
    } 
} 
+0

使用您的viewController作为CLLocationManagerDelegate那么你可以直接改变值'self.lbl_accuracy.text = accuracy'在行,你正在打印'打印(“精度= \(精度)”)' –

回答

1

您可以使用delegate design pattern发送位置精度家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") 
     } 
    } 
} 
0

Introduction to Key-Value Observing Programming Guide

Key-Value Observing

  1. NSNotification & NSNotificationCenter
  2. 键 - 值观察
  3. 代表
  4. 回调

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 
}