How to pass (grab) a Parse `PFUser` from `MapView` to another `ViewController` on Swift?

I'd like to grab info from a MKPointAnnotation on a MapView and send it to the next VC.

if I tap on a pin, bubble appears, shows a name and a disclosure button, but tapping the button I'd like to pass the specific PFUser to the next vc (maybe from self.testArrayUsers)

I don't know how to index in an array from the pin on map. Thanks in Advance for your help.

I tried by subclassing a MKPointAnnotation

import UIKit
import Parse
import MapKit
import CoreLocation

class MyMKPA: MKPointAnnotation {

    var pointAnnotationPFUser = PFUser()

}

in order to call it below, in func on 2/2 point, to grab the var selecteUserOnMapByTouchgPinVar and passing it to prepareForSegue

//MARK: this is for adding info button to pins on map 1/2
    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        var view = mapView.dequeueReusableAnnotationViewWithIdentifier("AnnotationView Id")

        if view == nil{
            view = MKPinAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView Id")
            view!.canShowCallout = true
        } else {
            view!.annotation = annotation
        }

        view?.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure)

        return view
    }

    //MARK: this is for adding info button to pins on map 2/2
    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

        if (control as? UIButton)?.buttonType == UIButtonType.DetailDisclosure {

            //            let selectedAnnTitle = mapView.selectedAnnotations[0].title!
            //            self.choosenUsername = selectedAnnTitle

             //***************************************************** 
              //this is what I had in mind, but I'm doing it very wrong, I think. 
              self.selecteUserOnMapByTouchgPinVar = mapView.selectedAnnotations[0].pointAnnotationPFUser 
             //***************************************************** 


            mapView.deselectAnnotation(view.annotation, animated: false)
//            performSegueWithIdentifier("testFromMapToUser", sender: view) //showListDetailView
        }
    }

in this PFQuery I put a PFUser inside each MKPointAnnotation

 func createAnnotations(point: PFGeoPoint, address: String)
    {
        let query = PFUser.query()

        query?.whereKey("location", nearGeoPoint: point, withinKilometers: withinKms)
        query?.orderByAscending("location")  //MARK:  Put list in order
        //MARK: Not include current user on the map
        let me = PFUser.currentUser()?.username
        query?.whereKey("username", notEqualTo: me!)
        query?.limit = self.kLimitNumberForQueryResults
        query?.findObjectsInBackgroundWithBlock({ (objects, error) -> Void in

            if error == nil
            {
                for(var i = 0; i < objects!.count; i++) {

                    let user = objects![i] as! PFUser

                    self.testArrayUsers.append((pickedUser : user, Name : user.username!)) //I would like to use this fot button in pin

//                    let myHomePin = MKPointAnnotation() //switched to MyMKPA
                    let myHomePin = MyMKPA()
                    let userPoint = user["location"] as! PFGeoPoint
                    myHomePin.coordinate = CLLocationCoordinate2DMake(userPoint.latitude, userPoint.longitude)
                    myHomePin.title = self.checkAndFindName(user)//user.username
                    //                    myHomePin.subtitle = address //problem : gives always my address, not retrived users' one
                    myHomePin.pointAnnotationPFUser = user
                    self.mapView.addAnnotation(myHomePin)
                    //                    self.closeUsersArray.append(user.username!) //MARK: Test

                }


                //                    println("this is the array of users * \(self.closeUsersArray) *") //MARK: Test
            }
            else
            {
                print("Error: " + error!.localizedDescription)
            }
        })
    }

Answers


I would create a property on the view controller to hold the PFUser object. For example:

var userToPass: PFUser?

func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

    let annotation = view.annotation as! MyMKPA
    self.userToPass = annotation.pointAnnotationPFUser

    self.performSegueWithIdentifier("MySegue", sender: self)
}

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
    // ...
    // myDestinationVC.passedUser = self.userToPass
}

Need Your Help

Synchronize content of website on NLB servers

iis load-balancing fileshare nlb

what would be the best way to have a website that is hosted on two distinct IIS servers which are load-balanced using Microsoft's NLB to sync the files that could be uploaded from the website's use...

ViewPager setCurrentItem freezes UI thread

android android-support-library

I am using a ViewPager from Android support v13 and I'd like to scroll to a specific item using setCurrentItem(int), but when I try to scroll more than 2 pages the application freezes, and after a ...