簡體   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