如何在 Swift 上将 Parse `PFUser` 从`MapView` 传递(抓取)到另一个`ViewController`?

Posted

技术标签:

【中文标题】如何在 Swift 上将 Parse `PFUser` 从`MapView` 传递(抓取)到另一个`ViewController`?【英文标题】:How to pass (grab) a Parse `PFUser` from `MapView` to another `ViewController` on Swift? 【发布时间】: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)
            
        )
    

【问题讨论】:

【参考方案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 的子类,那么不看代码就很难判断。

以上是关于如何在 Swift 上将 Parse `PFUser` 从`MapView` 传递(抓取)到另一个`ViewController`?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Swift 上将 HTML 代码加载到 WebView

Swift - 如何在 Firebase 上将排序查询创建为降序?

如何在swift 3上将UITableViewController的背景颜色更改为渐变色

如何在 iOS 上将“MongoDB 日期时间(ISO 日期)”解析为 NSDate(swift 和 Objective-c)

如何在 Swift 上将具有自定义 ID 的文档添加到 Firebase (Firestore)

在 Swift 上将所有手势从 UIView 转换为 UIScrollView