-swift-无法使用swift中的“披露”按钮将解析对象从地图套件注释传递到detailViewController

-swift-无法使用swift中的“披露”按钮将解析对象从地图套件注释传递到detailViewController,swift,parse-platform,mkannotation,mkannotationview,pfobject,Swift,Parse Platform,Mkannotation,Mkannotationview,Pfobject,请帮助,我无法将解析对象从注释查询传递到destinationViewController。当用户点击注释视图时,它会将用户带到destinationVC,但无论用户点击哪个注释,它都会传递相同的对象。我认为这与prepareForSegue函数有关。请让我知道我做错了什么 这是我的密码: class MapSearchViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate { @IBOutl

请帮助,我无法将解析对象从注释查询传递到destinationViewController。当用户点击注释视图时,它会将用户带到destinationVC,但无论用户点击哪个注释,它都会传递相同的对象。我认为这与prepareForSegue函数有关。请让我知道我做错了什么

这是我的密码:

class MapSearchViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

@IBOutlet weak var mapView: MKMapView!



var jobPosts = [PFObject]()
var jobPost: PFObject?


let locationManager = CLLocationManager()
var currentLoc: PFGeoPoint! = PFGeoPoint()


override func viewDidLoad() {
    super.viewDidLoad()

    //MARK new added code Start--

    self.mapView.delegate = self

    self.mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)

    //MARK new added code End--

    self.locationManager.delegate = self

    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest

    self.locationManager.requestWhenInUseAuthorization()

    self.locationManager.startUpdatingLocation()

    //Snippet For Blue Current Location Dot:

    //self.mapView.showsUserLocation = true



}


//MARK: -- Location Delegate Methods

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

    let location = locations.last

    let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

    let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))

    self.mapView.setRegion(region, animated: true)

    self.locationManager.stopUpdatingLocation()


}


func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
    print("Errors: " + error.localizedDescription)
}



//MARK: OCT 26

override func viewDidAppear(animated: Bool) {
    let annotationQuery = PFQuery(className: "JobPost")
    currentLoc = PFGeoPoint(location: locationManager.location)
    annotationQuery.whereKey("location", nearGeoPoint: currentLoc, withinMiles: 100)
    annotationQuery.findObjectsInBackgroundWithBlock {
        (posts, error) -> Void in
        if error == nil {
            // The find succeeded.
            print("Successful query for annotations")
            let jobPosts = posts as [PFObject]!

            for post in jobPosts {
                let point = post["location"] as! PFGeoPoint
                var annotation = MKPointAnnotation()
                annotation.coordinate = CLLocationCoordinate2DMake(point.latitude, point.longitude)
                self.mapView.addAnnotation(annotation)
                annotation.title = post["job"] as! String
                let pay = post["price"] as! String
                let payLabel = "\(pay)$"
                annotation.subtitle = payLabel
                let btn = UIButton(type: .DetailDisclosure)
                self.jobPost = post


            }
        } else {
            // Log details of the failure
            print("Error: \(error)")
        }
    }
}

func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
    var view = mapView.dequeueReusableAnnotationViewWithIdentifier("annotationIdentifier")
    if view == nil {
        view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "annotationIdentifier")
        view?.canShowCallout = true
        view?.rightCalloutAccessoryView = UIButton(type: .DetailDisclosure)
    } else {
        view?.annotation = annotation
    }
    return view
}

var selectedAnnotation: MKPointAnnotation!

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == view.rightCalloutAccessoryView {
        selectedAnnotation = view.annotation as? MKPointAnnotation
        performSegueWithIdentifier("annotationSender", sender: self)
    }
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    if let destination = segue.destinationViewController as? JobDetailViewController {
        destination.currentObject = jobPost
    }
}
}

detailVC的代码:

class JobDetailViewController: UIViewController, MFMailComposeViewControllerDelegate {

@IBOutlet weak var jobTitleLabel: UILabel!

@IBOutlet weak var priceLabel: UILabel!

@IBOutlet weak var distanceLabel: UILabel!

@IBOutlet weak var jobDescriptionContent: UITextView!

@IBOutlet weak var pictureImageView: UIImageView!


var jobPosts:[PFObject]!
var currentObject : PFObject?
var distance = "7"
var annotation = MKPointAnnotation()

override func viewDidLoad() {
    super.viewDidLoad()

    self.distanceLabel.text = "\(distance) miles away"

    if let object = currentObject {
        jobTitleLabel.text = object["job"] as! String
        priceLabel.text = object["price"] as! String
        jobDescriptionContent.text = object["description"] as! String
    }

}

看起来您有
jobPost
对象,该对象在方法
viewdide
中的这行代码
self.jobPost=post
中设置。当
prepareForSegue
在jobsposts{…}中对其进行所有冷却时,它具有来自
的最后一个对象值。如果需要从
jobsposts
中选择对象,则必须在用户执行操作时选择它。现在看起来你总是将最后一个对象设置为你的
detailVC
啊,我明白了,你说得对,它抓住了最后一个对象。当用户执行操作时,我将如何选择对象?您可以创建
MKPointAnnotation
的子类,并在地图上使用它。您可以在
mapView(mapView:MKMapView,viewForAnnotation注释:MKAnnotation)
方法中创建此对象。例如,可以向该子类添加字段
jobsindex
,并为添加到地图的每个注释将其设置为
jobsposts
中的索引。在
prepareforsgue(segue:UIStoryboardSegue,sender:AnyObject?
方法中,您可以从
sender
获取此索引,并从您的
jobsposts[索引]获取所需的对象。