我在watchOS上的2个接口控制器之间传递数据时遇到了一些困难,这是我的代码:
接口控制器
import WatchKit
import Foundation
import CoreLocation
class InterfaceController: WKInterfaceController, CLLocationManagerDelegate {
let locationManager : CLLocationManager = CLLocationManager()
private var lat : Double!
private var long : Double!
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.requestLocation()
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
let currentLocation: CLLocation = locations[0]
lat = currentLocation.coordinate.latitude
long = currentLocation.coordinate.longitude
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
{
print("Error: " + error.localizedDescription)
}
override func contextForSegueWithIdentifier(segueIdentifier: String) -> AnyObject? {
return [lat,long]
}
}
目的地控制器
import WatchKit
import Foundation
import MapKit
import CoreLocation
class ZEROPOINTFIVEInterfaceController: WKInterfaceController, CLLocationManagerDelegate {
@IBOutlet var mapViewZ: WKInterfaceMap!
let locationManager : CLLocationManager = CLLocationManager()
var lat: Double!
var long: Double!
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
if let strings = context as? [String] {
lat = Double(strings[0])
long = Double(strings[1])
}
let location = CLLocationCoordinate2D(latitude: lat, longitude: long)
let coordinateSpan = MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2)
mapViewZ.setRegion(MKCoordinateRegion(center: location, span: coordinateSpan))
// Configure interface objects here.
}
override func willActivate() {
// This method is called when watch view controller is about to be visible to user
super.willActivate()
}
override func didDeactivate() {
// This method is called when watch view controller is no longer visible
super.didDeactivate()
}
}
我想传递lat和long。但是当我在模拟器中运行它时,它是零。请帮忙。