将按钮添加到我的MKPointAnnotation

将按钮添加到我的MKPointAnnotation

本文介绍了Swift-将按钮添加到我的MKPointAnnotation的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Swift 1.2上:我想在地图上的注释中添加一个按钮.第一次点击图钉会显示用户的ID,并且这是可行的,但是然后,通过按一下按钮,我想将用户发送到详细信息视图控制器.

On Swift 1.2 : I'd like to add a button to my annotations on the map. First tap on pin shows user's id, and this is working, but then, by pressing the button, I want to send the user to a detail view controller.

您可以在此处此处

试图应用这些解决方案,但我想我缺少了一些东西.这是我正在寻找的另一个例子

tried to apply those solutions, but I think I'm missing something. This is another example of what I'm seeking

这是我的课堂宣言:

import UIKit
import MapKit
import CoreLocation
import Parse

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate, UIActionSheetDelegate {

这是代码中我要使用按钮创建注释的部分:

and this is the part of the code where I'd like to create annotations with buttons:

    // MARK: - Create Annotation

    func createAnnotations(point: PFGeoPoint, address: String)
    {
        println("*** this evaluates n.3 ***")
        var 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
        var 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
                    var myHomePin = MKPointAnnotation()
                    let userPoint = user["location"] as! PFGeoPoint
                    myHomePin.coordinate = CLLocationCoordinate2DMake(userPoint.latitude, userPoint.longitude)
                    myHomePin.title = user.username

 //MARK: core of the problem
//                    let detailButton : UIButton = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as! UIButton
//                    myHomePin.rightCalloutAccessoryView = detailButton
//




//                    myHomePin.subtitle = address
                    self.mapView.addAnnotation(myHomePin)
                    //                    self.closeUsersArray.append(user.username!) //MARK: Test

                }






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

        })

    }

提前谢谢!

推荐答案

使用"viewForAnnotation"地图视图委托方法设置左和右calloutAccessories.并且还替换了UIButton.buttonWithType,它将起作用:您需要的所有委托方法,但是我没有使用createAnnotation函数,因为您说它可以正常工作

Use the 'viewForAnnotation' map view delegate method to setup left and rightcalloutAccessories. and also UIButton.buttonWithType is replaced, this will work: all delegate methods you need but I didn't though your createAnnotation function because you said its working fine

class MapViewController: UIViewController, MKMapViewDelegate //
{
    @IBOutlet weak var mapView: MKMapView!{ //make sure this outlet is connected
        didSet{
            mapView.delegate = self
        }
    }

    // MARK: - Map view delegate

    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?.leftCalloutAccessoryView = nil
        view?.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure)
        //swift 1.2
        //view?.rightCalloutAccessoryView = UIButton.buttonWithType(UIButtonType.DetailDisclosure) as UIButton

        return view
    }

    func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
        //I don't know how to convert this if condition to swift 1.2 but you can remove it since you don't have any other button in the annotation view
        if (control as? UIButton)?.buttonType == UIButtonType.DetailDisclosure {
                mapView.deselectAnnotation(view.annotation, animated: false)
                performSegueWithIdentifier("you're segue Id to detail vc", sender: view)
            }
        }
    }

    //Your function to load the annotations in viewDidLoad

}

这篇关于Swift-将按钮添加到我的MKPointAnnotation的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 14:30