gpt4 book ai didi

swift - 制作多个引脚注释按钮以连接到另一个 View Controller (swift 3)

转载 作者:行者123 更新时间:2023-11-28 08:04:21 25 4
gpt4 key购买 nike

我的 map 上有两个图钉注释。我创建了一个信息按钮来执行转场。但该按钮仅出现在一个注释上。谁能帮帮我吗?对于注释,我创建了一个函数,并将其放入 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)
}
}
}

最佳答案

在查看您的源代码时,我无法直接看到答案,但我看到了一个紧迫的问题。你在 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? 代替。

关于swift - 制作多个引脚注释按钮以连接到另一个 View Controller (swift 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45591167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com