2017-06-03 9 views
0

Ich habe eine UI-Bezeichnung auf einem Viewcontroller und eine Zeichenfolge (Genauigkeit), die auf einem Projekt ist. Ich möchte die Zeichenfolge auf dem uilabel setzen, die ständig aktualisiert, da, wenn Sie die Zeichenfolge (Genauigkeit) drucken, es aktualisiert. Kann mir jemand helfen ? DankSo verbinden Sie UILabel mit einer anderen Klasse

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

Verwenden Sie Ihre Viewcontroller als CLLocationManagerDelegate dann können Sie direkt den Wert ändern 'self.lbl_accuracy.text = accuracy' in der Zeile, wo Sie gerade Druck' print ("Genauigkeit = \ (Genauigkeit)") ' –

Antwort

1

können Sie delegate design pattern verwenden Ortungsgenauigkeit zum Haupt VC zu senden. Erstellen Sie zuerst LocationUpdatable protocol und fügen Sie updateLocationAccuracy(_ accuracy: String) function hinzu. Erstellen Sie ein schwaches Delegatobjekt in der PBLocation-Klasse, und Sie können nun das Genauigkeitsobjekt über die updateLocationAccuracy-Methode an Home VC senden. Abschließend Bestätigen Sie das LocationUpdatable-Protokoll in Home VC und implementieren Sie die updateLocationAccuracy(_ accuracy: String)-Funktion, und legen Sie am wichtigsten den Home VC als Delegat des LocationUpdatable Delegiertenobjekts fest.

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. Schlüsselwert Beobachten
  3. Delegierten
  4. Rückrufe

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 
} 
Verwandte Themen