2017-08-09 80 views
1

我在地图上有两个注释注释。我创建了一个信息按钮来执行segue。但该按钮仅出现在一个注释上。任何人都可以帮助我吗?对于注释,我创建了一个放入viewdidload方法的函数。使多引脚注释按钮继续到其他视图控制器(swift 3)

Here`s代码:

import UIKit 
import MapKit 

class MapVC: UIViewController, MKMapViewDelegate { 

    @IBOutlet weak var mapView: MKMapView! 

    var locManager: CLLocationManager! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     meinkartenPunkt1() 
     meinkartenPunkt2() 

     locManager = CLLocationManager() 
     locManager.requestWhenInUseAuthorization() 

     // Do any additional setup after loading the view. 
     mapView.delegate = self  
    } 

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


    /* 
    // MARK: - Navigation 

    // In a storyboard-based application, you will often want to do a little preparation before navigation 
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 
     // Get the new view controller using segue.destinationViewController. 
     // Pass the selected object to the new view controller. 
    } 
    */ 

    func mapView(_ mapView: MKMapView,didUpdate userLocation: MKUserLocation){ 
     mapView.region.center=userLocation.coordinate 
     mapView.showAnnotations(mapView.annotations, animated: true) 
    } 

    func meinkartenPunkt1() { 
     let breite: CLLocationDegrees = 8.737653 
     let länge: CLLocationDegrees = 47.504333 

     let Koordinaten = CLLocationCoordinate2DMake(länge, breite) 

     let Span = MKCoordinateSpanMake(0.01, 0.01) 

     let Region = MKCoordinateRegionMake(Koordinaten, Span) 

     mapView.setRegion(Region, animated: true) 

     let Stecknadel = MKPointAnnotation() 
     Stecknadel.coordinate = Koordinaten 

     Stecknadel.title = "Heiligberg" 


     mapView.addAnnotation(Stecknadel) 
    } 

    func meinkartenPunkt2() { 
     let breite: CLLocationDegrees = 8.734345 
     let länge: CLLocationDegrees = 47.508456 

     let Koordinaten = CLLocationCoordinate2DMake(länge, breite) 

     let Span = MKCoordinateSpanMake(0.01, 0.01) 

     let Region = MKCoordinateRegionMake(Koordinaten, Span) 

     mapView.setRegion(Region, animated: true) 

     let Stecknadel = MKPointAnnotation() 
     Stecknadel.coordinate = Koordinaten 

     Stecknadel.title = "Kanti Rychenberg" 

     mapView.addAnnotation(Stecknadel) 
    } 

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView?{ 
     if annotation is MKUserLocation {return nil} 

     let reuseId = "pin" 

     var pinView = mapView.dequeueReusableAnnotationView(withIdentifier: reuseId) as? MKPinAnnotationView 
     if pinView == nil { 
      pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
      pinView!.canShowCallout = true 
      pinView!.animatesDrop = true 
      let calloutButton = UIButton(type: .detailDisclosure) 
      pinView!.rightCalloutAccessoryView = calloutButton 
      pinView!.sizeToFit() 
     } 
     else { 
      pinView!.annotation = annotation 
     } 

     return pinView 
    } 

    func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, 

    calloutAccessoryControlTapped control: UIControl) { 
      if control == view.rightCalloutAccessoryView { 
       performSegue(withIdentifier: "bookDetails", sender: self) 
      } 
     } 
} 

回答

0

当你的源代码看,我无法直接看到的答案,但是我看到一个迫在眉睫的问题。你在pinView上做了一些重要的设置,但它是零。说实话,这个代码肯定崩溃,如果曾达到它:

if pinView == nil { 
     pinView = MKPinAnnotationView(annotation: annotation, reuseIdentifier: reuseId) 
     pinView!.canShowCallout = true 

幸运的是,pinView永远是零然而,作为的MapView成功地创建了pinView。你可能想要做的是设置calloutButton,如果没有设置的话。因此,而不是if pinView == nil你写:

if pinView?.rightCalloutAccessoryView == nil { 
    // Initialize the rightCalloutAccessoryView here 
} 

提示:一般来说,你应该避免力!解开,因为它导致崩溃。尝试使用optional chaining而不是?

+0

谢谢你的回答。帮了一下。但我仍然无法弄清楚为什么按钮的功能只能用于一个注释... – georg

相关问题