地图注释在按下按钮后打开特定的 viewcontroller?

Map annotation open a specific viewcontroller after button pressed?

我想添加一个功能,当我按下特定引脚上的“详细信息披露”按钮时,它将打开特定的 viewController 谁能帮忙?

正如所见,我有很多 Pin 图,如果可能的话,我希望每个 Pin 图都能打开我通过故事板 ID 为它制作的特定视图控制器...

代码:

import UIKit
import MapKit
import CoreLocation





class MapViewController: UIViewController,MKMapViewDelegate, CLLocationManagerDelegate,UISearchBarDelegate{

    //=========SearchBarMap==========

    @IBAction func SearchBar(sender: AnyObject) {

        searchController = UISearchController(searchResultsController: nil)
        searchController.hidesNavigationBarDuringPresentation = false
        self.searchController.searchBar.delegate = self
        presentViewController(searchController, animated: true, completion: nil)
    }

    //==============================

    @IBOutlet weak var mapView: MKMapView!

    var searchController:UISearchController!
    var annotation:MKAnnotation!
    var localSearchRequest:MKLocalSearchRequest!
    var localSearch:MKLocalSearch!
    var localSearchResponse:MKLocalSearchResponse!
    var error:NSError!
    var pointAnnotation:MKPointAnnotation!
    var pinAnnotationView:MKPinAnnotationView!





    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()


        //==========RegionLocation : =========

        // Init the zoom level
        let coordinate:CLLocationCoordinate2D = CLLocationCoordinate2D(latitude: 31.30, longitude: 34.45)
        let span = MKCoordinateSpanMake(125, 125)
        let region = MKCoordinateRegionMake(coordinate, span)
        self.mapView.setRegion(region, animated: true)



        //====================================\


        //==============Tel Aviv==============
        self.mapView.delegate = self
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
        self.mapView.showsUserLocation = true


        let LitzmanLocation = CLLocationCoordinate2DMake(32.100668,34.775192)
        // Drop a pin
        let Litzman = MKPointAnnotation()
        Litzman.coordinate = LitzmanLocation
        Litzman.title = "Litzman Bar"
        Litzman.subtitle = "רחוב נמל תל אביב"
        mapView.addAnnotation(Litzman)

        let ShalvataLocation = CLLocationCoordinate2DMake(32.101145,34.775163)
        // Drop a pin
        let Shalvata = MKPointAnnotation()
        Shalvata.coordinate = ShalvataLocation
        Shalvata.title = "Shalvata Club"
        Shalvata.subtitle = "נמל תל אביב,האנגר 28"
        mapView.addAnnotation(Shalvata)

        let MarkidLocation = CLLocationCoordinate2DMake(32.074961,34.781679)
        // Drop a pin
        let Markid = MKPointAnnotation()
        Markid.coordinate = MarkidLocation
        Markid.title = "Markid Club"
        Markid.subtitle = "אבן גבירול 30,תל אביב"
        mapView.addAnnotation(Markid)

        let CatDogLocation = CLLocationCoordinate2DMake(32.068217,34.782978)
        // Drop a pin
        let CatDog = MKPointAnnotation()
        CatDog.coordinate = CatDogLocation
        CatDog.title = "The Cat & The Dog"
        CatDog.subtitle = "קרליבך 28,תל אביב"
        mapView.addAnnotation(CatDog)

        let KuLocation = CLLocationCoordinate2DMake(32.055073,34.775059)
        // Drop a pin
        let Ku = MKPointAnnotation()
        Ku.coordinate = KuLocation
        Ku.title = "Ku"
        Ku.subtitle = "דרך שלמה 117,תל אביב"
        mapView.addAnnotation(Ku)

        let TheBlockLocation = CLLocationCoordinate2DMake(32.054474,34.779812)
        // Drop a pin
        let TheBlock = MKPointAnnotation()
        TheBlock.coordinate = TheBlockLocation
        TheBlock.title = "The Block"
        TheBlock.subtitle = "דרך שלמה 157,תל אביב"
        mapView.addAnnotation(TheBlock)


        let LightHouseLocation = CLLocationCoordinate2DMake(32.100119,34.774614)
        // Drop a pin
        let LightHouse = MKPointAnnotation()
        LightHouse.coordinate = LightHouseLocation
        LightHouse.title = "Light House"
        LightHouse.subtitle = "נמל תל אביב,האנגר 28"
        mapView.addAnnotation(LightHouse)

        let LangaClubLocation = CLLocationCoordinate2DMake(32.072964,34.768384)
        // Drop a pin
        let LangaClub = MKPointAnnotation()
        LangaClub.coordinate = LangaClubLocation
        LangaClub.title = "Langa Club - Valium"
        LangaClub.subtitle = "בן יהודה 1,תל אביב"
        mapView.addAnnotation(LangaClub)

        let Haoman17Location = CLLocationCoordinate2DMake(32.054180,34.766228)
        // Drop a pin
        let Haoman17 = MKPointAnnotation()
        Haoman17.coordinate = Haoman17Location
        Haoman17.title = "Haoman 17"
        Haoman17.subtitle = "אברבאנל 88,תל אביב"
        mapView.addAnnotation(Haoman17)


        let MorfiumClubLocation = CLLocationCoordinate2DMake(32.075300,34.782390)
        // Drop a pin
        let MorfiumClub = MKPointAnnotation()
        MorfiumClub.coordinate = MorfiumClubLocation
        MorfiumClub.title = "Morfium Club"
        MorfiumClub.subtitle = "שדרות שאול המלך 2,תל אביב"
        mapView.addAnnotation(MorfiumClub)

        let ClaraClubLocation = CLLocationCoordinate2DMake(32.067438,34.761854)
        // Drop a pin
        let ClaraClub = MKPointAnnotation()
        ClaraClub.coordinate = ClaraClubLocation
        ClaraClub.title = "Clara Club"
        ClaraClub.subtitle = "הדולפינריום,תל אביב"
        mapView.addAnnotation(ClaraClub)

        let BreakfastClubLocation = CLLocationCoordinate2DMake(32.062963,34.769649)
        // Drop a pin
        let BreakfastClub = MKPointAnnotation()
        BreakfastClub.coordinate = BreakfastClubLocation
        BreakfastClub.title = "Breakfast Club"
        BreakfastClub.subtitle = "רוטשילד 6,תל אביב"
        mapView.addAnnotation(BreakfastClub)



        let RadioEPGBLocation = CLLocationCoordinate2DMake(32.063020,34.775020)
        // Drop a pin
        let RadioEPGB = MKPointAnnotation()
        RadioEPGB.coordinate = RadioEPGBLocation
        RadioEPGB.title = "Radio EPGB"
        RadioEPGB.subtitle = "שד׳׳ל 7,תל אביב"
        mapView.addAnnotation(RadioEPGB)

        let FriendsCampusLocation = CLLocationCoordinate2DMake(32.086001,34.778461)
        // Drop a pin
        let FriendsCampus = MKPointAnnotation()
        FriendsCampus.coordinate = FriendsCampusLocation
        FriendsCampus.title = "Friends Campus Bar"
        FriendsCampus.subtitle = "בן יהודה 59,תל אביב"
        mapView.addAnnotation(FriendsCampus)

        let DuduBarLocation = CLLocationCoordinate2DMake(32.064437,34.771246)
        // Drop a pin
        let DuduBar = MKPointAnnotation()
        DuduBar.coordinate = DuduBarLocation
        DuduBar.title = "Dudu Bar"
        DuduBar.subtitle = "נחלת בנימין 40,תל אביב"
        mapView.addAnnotation(DuduBar)

        let PoliBarLocation = CLLocationCoordinate2DMake(32.064451,34.776245)
        // Drop a pin
        let PoliBar = MKPointAnnotation()
        PoliBar.coordinate = PoliBarLocation
        PoliBar.title = "Poly Bar"
        PoliBar.subtitle = "רוטשילד 60,תל אביב"
        mapView.addAnnotation(PoliBar)

        let BiggyZLocation = CLLocationCoordinate2DMake(32.077161,34.769328)
        // Drop a pin
        let BiggyZ = MKPointAnnotation()
        BiggyZ.coordinate = BiggyZLocation
        BiggyZ.title = "Biggy Z Bar"
        BiggyZ.subtitle = "בוגרשוב 18,תל אביב"
        mapView.addAnnotation(BiggyZ)

        let FlameLocation = CLLocationCoordinate2DMake(32.064410,34.771140)
        // Drop a pin
        let Flame = MKPointAnnotation()
        Flame.coordinate = FlameLocation
        Flame.title = "Flame Bar"
        Flame.subtitle = "נחלת בנימין 42,תל אביב"
        mapView.addAnnotation(Flame)


        let JoeyBarLocation = CLLocationCoordinate2DMake(32.072360,34.768870)
        // Drop a pin
        let JoeyBar = MKPointAnnotation()
        JoeyBar.coordinate = JoeyBarLocation
        JoeyBar.title = "Joey's Bar"
        JoeyBar.subtitle = "אלנבי 42,תל אביב"
        mapView.addAnnotation(JoeyBar)

        let ChaserBarLocation = CLLocationCoordinate2DMake(32.072032,34.769050)
        // Drop a pin
        let ChaserBar = MKPointAnnotation()
        ChaserBar.coordinate = ChaserBarLocation
        ChaserBar.title = "Chaser Bar"
        ChaserBar.subtitle = "אלנבי 44,תל אביב"
        mapView.addAnnotation(ChaserBar)

        let NextDoorBarLocation = CLLocationCoordinate2DMake(32.079237,34.780051)
        // Drop a pin
        let NextDoorBar = MKPointAnnotation()
        NextDoorBar.coordinate = NextDoorBarLocation
        NextDoorBar.title = "Next Door Bar"
        NextDoorBar.subtitle = "שדרות חן 52,תל אביב"
        mapView.addAnnotation(NextDoorBar)

        let AgnesBarLocation = CLLocationCoordinate2DMake(32.088654,34.782227)
        // Drop a pin
        let AgnesBar = MKPointAnnotation()
        AgnesBar.coordinate = AgnesBarLocation
        AgnesBar.title = "Agnes Pub"
        AgnesBar.subtitle = "אבן גבירול 129,תל אביב"
        mapView.addAnnotation(AgnesBar)

        let GiyoraBarLocation = CLLocationCoordinate2DMake(32.076015,34.775049)
        // Drop a pin
        let GiyoraBar = MKPointAnnotation()
        GiyoraBar.coordinate = GiyoraBarLocation
        GiyoraBar.title = "Bar Giyora"
        GiyoraBar.subtitle = "בר גיורא 4,תל אביב"
        mapView.addAnnotation(GiyoraBar)

        let PenguinClubLocation = CLLocationCoordinate2DMake(32.061882,34.772904)
        // Drop a pin
        let PenguinClub = MKPointAnnotation()
        PenguinClub.coordinate = PenguinClubLocation
        PenguinClub.title = "Penguin Club"
        PenguinClub.subtitle = "יהודה הלוי 43,תל אביב"
        mapView.addAnnotation(PenguinClub)

        let SoloBarLocation = CLLocationCoordinate2DMake(32.061671,34.773292)
        // Drop a pin
        let SoloBar = MKPointAnnotation()
        SoloBar.coordinate = SoloBarLocation
        SoloBar.title = "Solo Bar"
        SoloBar.subtitle = "יהודה הלוי 46,תל אביב"
        mapView.addAnnotation(SoloBar)

        let LimaLimaLocation = CLLocationCoordinate2DMake(32.062327,34.772290)
        // Drop a pin
        let LimaLima = MKPointAnnotation()
        LimaLima.coordinate = LimaLimaLocation
        LimaLima.title = "Lima Lima Bar"
        LimaLima.subtitle = "לילינבלום 42,תל אביב"
        mapView.addAnnotation(LimaLima)

        let BillyJinLocation = CLLocationCoordinate2DMake(32.062702,34.770186)
        // Drop a pin
        let BillyJin = MKPointAnnotation()
        BillyJin.coordinate = BillyJinLocation
        BillyJin.title = "Billie Jean Bar"
        BillyJin.subtitle = "רוטשילד 10,תל אביב"
        mapView.addAnnotation(BillyJin)

        let TwentyTwoLocation = CLLocationCoordinate2DMake(32.063163,34.771804)
        // Drop a pin
        let TwentyTwo = MKPointAnnotation()
        TwentyTwo.coordinate = TwentyTwoLocation
        TwentyTwo.title = "Twenty Two Club"
        TwentyTwo.subtitle = "רוטשילד 21,תל אביב"
        mapView.addAnnotation(TwentyTwo)

        let ZoiferLocation = CLLocationCoordinate2DMake(32.110905,34.839089)
        // Drop a pin
        let Zoifer = MKPointAnnotation()
        Zoifer.coordinate = ZoiferLocation
        Zoifer.title = "Zoifer Bar"
        Zoifer.subtitle = "ראול וולנברג 24,תל אביב"
        mapView.addAnnotation(Zoifer)

        let RubiBarLocation = CLLocationCoordinate2DMake(32.095287,34.774481)
        // Drop a pin
        let RubiBar = MKPointAnnotation()
        RubiBar.coordinate = RubiBarLocation
        RubiBar.title = "Rubi Bar"
        RubiBar.subtitle = "ירמיהו 7,תל אביב"
        mapView.addAnnotation(RubiBar)

        let ShuffleBarLocation = CLLocationCoordinate2DMake(32.056286,34.769090)
        // Drop a pin
        let ShuffleBar = MKPointAnnotation()
        ShuffleBar.coordinate = ShuffleBarLocation
        ShuffleBar.title = "Shuffle Bar"
        ShuffleBar.subtitle = "פלורנטין 19,תל אביב"
        mapView.addAnnotation(ShuffleBar)

        let KuliAlmaLocation = CLLocationCoordinate2DMake(32.062244,34.774771)
        // Drop a pin
        let KuliAlma = MKPointAnnotation()
        KuliAlma.coordinate = KuliAlmaLocation
        KuliAlma.title = "Kuli Alma"
        KuliAlma.subtitle = "מקווה ישראל 10,תל אביב"
        mapView.addAnnotation(ShuffleBar)

        let OttoRotchildLocation = CLLocationCoordinate2DMake(32.064194,34.774541)
        // Drop a pin
        let OttoRotchild = MKPointAnnotation()
        OttoRotchild.coordinate = OttoRotchildLocation
        OttoRotchild.title = "Otto Rothschild"
        OttoRotchild.subtitle = "רוטשילד 45,תל אביב"
        mapView.addAnnotation(OttoRotchild)

        let AlmaTheCubaLocation = CLLocationCoordinate2DMake(32.074140,34.795910)
        // Drop a pin
        let AlmaTheCuba = MKPointAnnotation()
        AlmaTheCuba.coordinate = AlmaTheCubaLocation
        AlmaTheCuba.title = "Alma The Cuba Bar"
        AlmaTheCuba.subtitle = "יגאל אלון 126,תל אביב"
        mapView.addAnnotation(AlmaTheCuba)

        let MyPubLocation = CLLocationCoordinate2DMake(32.079902,34.769263)
        // Drop a pin
        let MyPub = MKPointAnnotation()
        MyPub.coordinate = MyPubLocation
        MyPub.title = "My Pub"
        MyPub.subtitle = "פרישמן 15,תל אביב"
        mapView.addAnnotation(MyPub)

        let MagdalenaLocation = CLLocationCoordinate2DMake(32.061616,34.773257)
        // Drop a pin
        let Magdalena = MKPointAnnotation()
        Magdalena.coordinate = MagdalenaLocation
        Magdalena.title = "Magdalena Club"
        Magdalena.subtitle = "דה פיג׳וטו 22,תל אביב"
        mapView.addAnnotation(Magdalena)

        let JimmyWhoLocation = CLLocationCoordinate2DMake(32.062920,34.771961)
        // Drop a pin
        let JimmyWho = MKPointAnnotation()
        JimmyWho.coordinate = JimmyWhoLocation
        JimmyWho.title = "Jimmy Who Bar"
        JimmyWho.subtitle = "רוטשילד 24,תל אביב"
        mapView.addAnnotation(JimmyWho)

        //===================================\



        NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(didBecomeActive), name: UIApplicationDidBecomeActiveNotification, object: nil)










    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    //Dispose of resources that can be re created.

        }

    //Mark : Location

    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])



    {
        let location = locations.last

        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)

        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.06, longitudeDelta: 0.06))

        self.mapView.setRegion(region, animated: true)

        self.locationManager.stopUpdatingLocation()
    }

    func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
    {
        print("Errors: " + error.localizedDescription)
    }

    func mapView(mapView: MKMapView, viewForAnnotation annotation: MKAnnotation) -> MKAnnotationView? {
        if annotation is MKUserLocation {
            return nil
        }
        let reuseID = "pin"
        var pinView = mapView.dequeueReusableAnnotationViewWithIdentifier(reuseID) as? MKPinAnnotationView
        if(pinView == nil) {
            pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseID)
            pinView!.canShowCallout = true
            pinView!.animatesDrop = true
            pinView!.rightCalloutAccessoryView = UIButton(type: UIButtonType.DetailDisclosure) as UIButton
        }
        else
        {
            pinView!.annotation = annotation
        }


        return pinView

    }

    func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {

        if control == annotationView.rightCalloutAccessoryView {



        }
    }

    func didBecomeActive() {
    let annotations = self.mapView.annotations
    self.mapView.removeAnnotations(annotations)
    self.mapView.addAnnotations(annotations)
}

    //============SearchBarMap============\

    func searchBarSearchButtonClicked(searchBar: UISearchBar){
        //1
        searchBar.resignFirstResponder()
        dismissViewControllerAnimated(true, completion: nil)
        if self.mapView.annotations.count != 0{
            annotation = self.mapView.annotations[0]
            self.mapView.removeAnnotation(annotation)
        }
        //2
        localSearchRequest = MKLocalSearchRequest()
        localSearchRequest.naturalLanguageQuery = searchBar.text
        localSearch = MKLocalSearch(request: localSearchRequest)
        localSearch.startWithCompletionHandler { (localSearchResponse, error) -> Void in

            if localSearchResponse == nil{
                let alertController = UIAlertController(title: nil, message: "Place Not Found", preferredStyle: UIAlertControllerStyle.Alert)
                alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default, handler: nil))
                self.presentViewController(alertController, animated: true, completion: nil)
                return
            }
            //3
            self.pointAnnotation = MKPointAnnotation()
            self.pointAnnotation.title = searchBar.text
            self.pointAnnotation.coordinate = CLLocationCoordinate2D(latitude: localSearchResponse!.boundingRegion.center.latitude, longitude:     localSearchResponse!.boundingRegion.center.longitude)


            self.pinAnnotationView = MKPinAnnotationView(annotation: self.pointAnnotation, reuseIdentifier: nil)
            self.mapView.centerCoordinate = self.pointAnnotation.coordinate
            self.mapView.addAnnotation(self.pinAnnotationView.annotation!)

    //=============SearchBarMap===============\
        }
    }

}

使用 UIViewControllerperformSegueWithIdentifier 方法以编程方式对视图控制器执行 segue。 segueID的值应该是storyboard中segue的ID。

func mapView(MapView: MKMapView, annotationView: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
    if control == annotationView.rightCalloutAccessoryView {
        let segueID: String = "abc" // Use the appropriate segue ID here
        performSegueWithIdentifier(segueID, sender: self)
    }
}