2015-11-30 50 views
0

我想从MapView上的MKPointAnnotation获取信息并将其发送给下一个VC。如何从MapView传递(抓取)一个Parse`PFUser`到Swift上的另一个`ViewController`?

如果我的销上点击,出现气泡,示出了一个名称和一个公开键,但轻敲按钮我想具体PFUser传递到下一个VC(也许从self.testArrayUsers

我不不知道如何从地图上的图钉索引数组。在此先感谢您的帮助。

我试图通过继承一个MKPointAnnotation

import UIKit 
import Parse 
import MapKit 
import CoreLocation 

class MyMKPA: MKPointAnnotation { 

    var pointAnnotationPFUser = PFUser() 

} 

以低于调用它,在FUNC上2/2点,抢了var selecteUserOnMapByTouchgPinVar并将它传递给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 
     } 
    } 

在这PFQuery我把PFUser每个里面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) 
      } 
     }) 
    } 

enter image description here

回答

0

我会在视图控制器上创建一个属性来保存PFUser对象。例如:

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 
} 
+0

测试它!但有一个问题,在下一个vc中,应该抓住'userToPass'的var'user'是我的自定义类型'User',所以我有问题使用let nextVC:testMapUserVC = segue.destinationViewController! testMapUserVC if self.userToPass!= nil nextVC.user = self.userToPass as!用户//总是失败 } else { print(“error”) } – biggreentree

+0

那么你可以尝试在你的自定义类的'var userToPass:User?'类的顶部做变量。如果你的自定义类是PFUser的子类,那么很难说没有看到代码。 – Aaron

相关问题