-swift-使用swift中的公开按钮将parse对象从map kit注释传递给detailViewController

时间:2015-11-02 18:47:26

标签: swift parse-platform mkannotation mkannotationview pfobject

请帮助,我无法将parse对象从注释Query传递到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
    }

}

0 个答案:

没有答案