2017-06-03 48 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 
}