我正在使用包含注释的mapview。当按下注释上的信息按钮时,我查询我的cloudkit数据库并找到与按下的注释同名的记录。
然后我将从cloudkit返回的数据分配给变量,以便我可以通过segue将它们发送到detailViewController。
我知道数据是从查询中返回的,因为我可以在segue之前打印它。但是,没有任何数据似乎通过segue,这会在解开nil时导致detailView出错。
我觉得与NSOperationQueue有关,因为我对此并不了解。但我不确定。
以下是我正在使用的代码......
func mapView(mapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
let cloudContainer = CKContainer.defaultContainer()
let publicData = cloudContainer.publicCloudDatabase
let tappedPlace = annotationView.annotation!.title!! as String
let predi = NSPredicate(format: "Name = %@", tappedPlace)
let iquery = CKQuery(recordType: "Locations", predicate: predi)
publicData.performQuery(iquery, inZoneWithID: nil, completionHandler: {
(results, error) -> Void in
if error != nil {
print(error)
return
}
if let results = results {
print("Downloaded data for selected location for \(tappedPlace)")
NSOperationQueue.mainQueue().addOperationWithBlock() {
if(results.count > 0){
let dataToSend = results[0]
self.placeLocation = dataToSend.objectForKey("Location") as! CLLocation
self.placeImage = dataToSend.objectForKey("Image1") as! CKAsset
self.placeName = dataToSend.objectForKey("Name") as! String
self.placeCity = dataToSend.objectForKey("City") as! String
self.placeStory = dataToSend.objectForKey("Story") as! String
self.performSegueWithIdentifier("fromMap", sender: self)
}
}
}
})
}
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if segue.identifier == "fromMap" {
if let destinationController = segue.destinationViewController as? DetailViewController {
destinationController.loadedName = self.placeName
destinationController.loadedCity = self.placeCity
destinationController.loadedStory = self.placeStory
destinationController.loadedLocation = self.placeLocation
}
}
}
}