如何从每个注释视图创建自定义标注按钮?

时间:2016-01-27 12:47:20

标签: ios swift parse-platform annotations mapkit

我尝试在每个注释视图中创建一个按钮,以便将用户带到自定义到所选注释的新页面。我已成功实现了mapview以显示带有从我的解析数据库加载的标题和副标题的注释,但我很难找到一种方法:1)在注释视图中添加一个按钮以将用户带到一个新视图2)找到一种方法在选择时为每个注释创建自定义视图。任何帮助将不胜感激?

代码

class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!

    var MapViewLocationManager:CLLocationManager! = CLLocationManager()
    let btn = UIButton(type: .DetailDisclosure)


    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.showsUserLocation = true
        mapView.delegate = self
        MapViewLocationManager.delegate = self
        MapViewLocationManager.startUpdatingLocation()
        mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)

    }

    override func viewDidAppear(animated: Bool) {
        let annotationQuery = PFQuery(className: "Clubs")
        annotationQuery.findObjectsInBackgroundWithBlock {
            (clubs, error) -> Void in
            if error == nil {
                // The find succeeded.
                print("Successful query for annotations")
                // Do something with the found objects
                let myClubs = clubs! as [PFObject]
                for club in myClubs {

                    //data for annotation
                    let annotation = MKPointAnnotation()
                    let place = club["location"] as? PFGeoPoint
                    let clubName = club["clubName"] as? String
                    let stadiumName = club["stadium"] as? String
                    annotation.title = clubName
                    annotation.subtitle = stadiumName
                    annotation.coordinate = CLLocationCoordinate2DMake(place!.latitude,place!.longitude)

                    //add annotations
                    self.mapView.addAnnotation(annotation)
                }


            } else {
                // Log details of the failure
                print("Error: \(error)")
            }
        }

    }

2 个答案:

答案 0 :(得分:0)

这是Objective-C解决方案,希望它对您有所帮助。 要将button设置为annotationView

    UIButton *detailButton = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
    annotationView.rightCalloutAccessoryView = detailButton;

对此按钮执行click时,请按照delegate之类的方法进行操作,因此也会覆盖以下方法,

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control

您可以在上述方法中执行任何自定义操作,因为您知道用户现在已经点击了附件视图。

答案 1 :(得分:0)

For 1st - 你可以使用leftCalloutAccessoryView& annoationView上的rightCalloutAccessoryView。您必须实现MapViewDelegate MapView类(_:viewForAnnotation:)。

For 2nd - 与calloutAccessoryView类似,您可以访问annoationView上的detailCalloutAccessoryView。您可以使用它来自定义标注。