我为餐厅创建了一个自定义类,其中包括以下信息:名称,类型,地址,坐标,照片,收藏夹菜等。我制作了MKPinAnnotations,当名称和类型通过菜单上的按钮传递时,该名称和类型会传递单击了标注,但我想传递该类实例中的所有信息。我试图创建某种填充函数,但是每次找到一个nil值并且程序中断。我是Xcode的新手,欢迎任何帮助!
func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) ->
MKAnnotationView? {
if annotation is MKUserLocation {
return nil
}
let pin = MKPinAnnotationView(annotation: annotation,
reuseIdentifier: "pin")
pin.canShowCallout = true
pin.rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
return pin
}
func mapView(_ mapView: MKMapView, annotationView view:
MKAnnotationView, calloutAccessoryControlTapped control: UIControl)
{
let annView = view.annotation
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let detailVC = storyboard.instantiateViewController(withIdentifier:
"details") as! DetailViewController
detailVC.name = ((annView?.title)!)!
detailVC.type = ((annView?.subtitle)!)!
detailVC.fill2()
// fill2()是我在detailViewController中创建的函数
self.navigationController?.pushViewController(detailVC, animated:
true)
}
DetailViewController函数:
func fill2(){
if detailName.text == places[0].name {
detailFavorite.text = places[0].favorite
detailAddress.text = places[0].address
detailType.text = places[0].type
detailImage.image = places[0].photo
}
}
欢迎任何帮助!谢谢!