Swift 使用注释标注调用 segue
Swift using annotation callout to call segue
我正在开发一个应用程序,您可以在其中看到一些 pins/annotations,当您单击它们时,它们会显示有关该地点的一些信息。我想要做的是添加一个按钮,这样当用户单击 "callout" 时,它会激活一个 segue,它将打开一个包含更多详细信息的新视图。
我有我的代码和功能,但它似乎是一个错误,因为当我点击按钮 运行 转场时,应用程序崩溃了。我的代码如下...
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var pin = MKPointAnnotation()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.showsUserLocation = true
//Adding information to my Annotation
let pinCoordenadas = CLLocationCoordinate2DMake(25.589339000, -100.257724800)
pin.coordinate = pinCoordenadas
pin.title = "Christus Muguerza"
mapView.addAnnotation(pin)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is MKPointAnnotation) {
print("Not MKPointAnnotation")
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier")
if annotationView == nil{
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
annotationView!.canShowCallout = true
annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
else {
annotationView!.annotation = annotation
}
annotationView!.image = UIImage(named: "curz")
return annotationView
}
var anotacionSeleccionada : MKPointAnnotation!
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
anotacionSeleccionada = view.annotation as? MKPointAnnotation
performSegue(withIdentifier: "vista", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ViewController {
destination.pin = anotacionSeleccionada
}
}
}
错误...
我希望你们能帮助我,我真的很感激你们的帮助。谢谢
您 mapView
的出口似乎坏了,因为您的程序在 viewDidLoad
的第一行崩溃了。这样做来解决这个问题:
- 转到您的故事板
- 右键单击您的地图
- 删除 outlet/outlets 如果您错误地创建了多个
- 从您的代码中删除
@IBOutlet weak var mapView: MKMapView!
- 创建新出口
- 运行 再次输入您的代码
问题是 OP 的 mainView 和 detailView 都连接到文件 viewController。我为 OP 创建了一个名为 DetailController 的新文件,并链接了第二个 viewController,现在它可以正常工作了。
我正在开发一个应用程序,您可以在其中看到一些 pins/annotations,当您单击它们时,它们会显示有关该地点的一些信息。我想要做的是添加一个按钮,这样当用户单击 "callout" 时,它会激活一个 segue,它将打开一个包含更多详细信息的新视图。
我有我的代码和功能,但它似乎是一个错误,因为当我点击按钮 运行 转场时,应用程序崩溃了。我的代码如下...
class ViewController: UIViewController, MKMapViewDelegate {
@IBOutlet weak var mapView: MKMapView!
var pin = MKPointAnnotation()
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate = self
mapView.showsUserLocation = true
//Adding information to my Annotation
let pinCoordenadas = CLLocationCoordinate2DMake(25.589339000, -100.257724800)
pin.coordinate = pinCoordenadas
pin.title = "Christus Muguerza"
mapView.addAnnotation(pin)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
if !(annotation is MKPointAnnotation) {
print("Not MKPointAnnotation")
}
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "identifier")
if annotationView == nil{
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "identifier")
annotationView!.canShowCallout = true
annotationView!.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
}
else {
annotationView!.annotation = annotation
}
annotationView!.image = UIImage(named: "curz")
return annotationView
}
var anotacionSeleccionada : MKPointAnnotation!
func mapView(_ mapView: MKMapView, annotationView view: MKAnnotationView, calloutAccessoryControlTapped control: UIControl) {
if control == view.rightCalloutAccessoryView {
anotacionSeleccionada = view.annotation as? MKPointAnnotation
performSegue(withIdentifier: "vista", sender: self)
}
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destination = segue.destination as? ViewController {
destination.pin = anotacionSeleccionada
}
}
}
错误...
我希望你们能帮助我,我真的很感激你们的帮助。谢谢
您 mapView
的出口似乎坏了,因为您的程序在 viewDidLoad
的第一行崩溃了。这样做来解决这个问题:
- 转到您的故事板
- 右键单击您的地图
- 删除 outlet/outlets 如果您错误地创建了多个
- 从您的代码中删除
@IBOutlet weak var mapView: MKMapView!
- 创建新出口
- 运行 再次输入您的代码
问题是 OP 的 mainView 和 detailView 都连接到文件 viewController。我为 OP 创建了一个名为 DetailController 的新文件,并链接了第二个 viewController,现在它可以正常工作了。