【问题标题】:How to pass (grab) a Parse `PFUser` from `MapView` to another `ViewController` on Swift?如何在 Swift 上将 Parse `PFUser` 从`MapView` 传递(抓取)到另一个`ViewController`?
【发布时间】:2015-11-30 01:27:48
【问题描述】:

我想从 MapView 上的 MKPointAnnotation 中获取信息并将其发送给下一个 VC。

如果我点击一个图钉,会出现气泡,显示一个名称和一个披露按钮,但点击该按钮我想将特定的 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 中,我在每个 MKPointAnnotation 中添加了一个 PFUser

 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)
            }
        })
    }

【问题讨论】:

    标签: ios swift parse-platform


    【解决方案1】:

    我会在视图控制器上创建一个属性来保存 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
    }
    

    【讨论】:

    • 测试它!但是有一个问题,在下一个 vc 中,应该抓取 userToPass 的 var user 是我的自定义类型 User 所以我在使用 let nextVC 向下转换时遇到问题:testMapUserVC = segue.destinationViewController as! testMapUserVC if self.userToPass != nil { nextVC.user = self.userToPass as!用户 //总是失败 } else { print("error") }
    • 您可以尝试将类顶部的变量设为您的自定义类var userToPass: User?。如果您的自定义类是 PFUser 的子类,那么不看代码就很难判断。
    猜你喜欢
    • 2014-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多