繁体   English   中英

通过点击地图注释上的右侧附件标注按钮来快速选择视图

[英]Swift how to segue views by tapping a right accessory callout button on a map annotation

我对快速进行面向对象的编程非常陌生,我试图通过点击长按掉下的图钉注释中的右侧辅助标注按钮,将视图从包含地图的视图切换到另一个视图。 在情节提要中,我在两个视图之间创建了一个序列,并为序列分配了标识符mapseg。 不幸的是,在尝试了所有我可以通过Google找到的东西之后,当我点击正确的附件标注按钮并且不知道为什么时,我就无法进行选择。该应用程序本身是一个带有三个选项卡的选项卡式应用程序。 第二个选项卡的视图是包含地图的视图。 另外,我不知道这是否与它有关,但是我尝试从中进行转换的视图嵌入在导航控制器中。 这是我要从中转换视图的代码。

import UIKit
import MapKit

class SecondViewController: UIViewController, MKMapViewDelegate {

    @IBOutlet weak var MapView: MKMapView!
    override func viewDidLoad() {
        super.viewDidLoad()
        self.MapView.delegate = self
        // Do any additional setup after loading the view, typically from a nib.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        let reuseID = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
        pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
        pinView!.canShowCallout = true
        pinView!.animatesDrop = true
        pinView!.enabled=true
        let btn = UIButton(type: .DetailDisclosure)
        btn.titleForState(UIControlState.Normal)
        pinView!.rightCalloutAccessoryView = btn as UIView
        return pinView
    }


    @IBAction func dropPin(sender: UILongPressGestureRecognizer) {
        if sender.state == UIGestureRecognizerState.Began {
            let location = sender.locationInView(self.MapView)
            let locCoord = self.MapView.convertPoint(location, toCoordinateFromView: self.MapView)
            let annotation = MKPointAnnotation()

            annotation.coordinate = locCoord
            annotation.title = "City Name"
            annotation.subtitle = ""

            self.MapView.removeAnnotations(MapView.annotations)
            self.MapView.addAnnotation(annotation)
            self.MapView.selectAnnotation(annotation, animated: true)


        }
        func mapView(mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
            if control == view.rightCalloutAccessoryView {
                self.performSegueWithIdentifier("mapseg", sender: self)

            }
        }

    }

}

我认为您需要重写prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?)方法。 并检查是否工作

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM