【问题标题】:Make mutliple pin annotations buttons to segue to an other view controller (swift 3)制作多个引脚注释按钮以连接到另一个视图控制器(swift 3)
【发布时间】:2018-01-17 08:56:00
【问题描述】:

我的地图上有两个大头针注释。我创建了一个信息按钮来执行 segue。但是该按钮仅出现在一个注释上。任何人都可以帮助我吗?对于注释,我创建了一个放入 viewdidload 方法的函数。

代码如下:

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)
            }
        }
}

【问题讨论】:

    标签: swift button annotations mapkit segue


    【解决方案1】:

    查看您的源代码时,我无法直接看到答案,但我看到了一个直接的问题。你在 pinView 上做了一些重要的设置,虽然它是 nil。老实说,如果达到此代码肯定会崩溃:

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

    幸运的是,pinView 永远不会是 nil,因为 mapView 成功创建了 pinView。您可能想要做的是设置 calloutButton(如果未设置)。所以你写的不是if pinView == nil

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

    提示:一般情况下,您应该避免使用! 强制解包,因为这会导致崩溃。尝试将optional chaining? 一起使用。

    【讨论】:

    • 感谢您的回答。有点帮助。但我还是不明白为什么按钮的功能只适用于一个注释......
    猜你喜欢
    • 2017-03-06
    • 1970-01-01
    • 2013-05-01
    • 1970-01-01
    • 2017-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多