J'ai un code que j'ai trouvé en ligne qui correspond parfaitement à mon projet. Le problème est que je n'utilise pas de story-boards du tout. Au lieu d'utiliser un fichier de storyboard pour créer le UIView (CustomCallOutView)
, je viens de créer une classe avec une sous-classe de UIView. Voici le code qui nécessite un fichier nib mais je ne veux pas les utiliser. Comment puis-je y parvenir? Le code est ci-dessous. Je vous remercie!Swift 4: Quel est l'équivalent de charger un fichier nib par programme, sans story-boards?
func mapView(_ mapView: MKMapView,
didSelect view: MKAnnotationView)
{
// 1
if view.annotation is MKUserLocation
{
// Don't proceed with custom callout
return
}
// 2
let starbucksAnnotation = view.annotation as! StarbucksAnnotation
let views = Bundle.main.loadNibNamed("CustomCalloutView", owner: nil, options: nil)
let calloutView = views?[0] as! CustomCalloutView
calloutView.starbucksName.text = starbucksAnnotation.name
calloutView.starbucksAddress.text = starbucksAnnotation.address
calloutView.starbucksPhone.text = starbucksAnnotation.phone
calloutView.starbucksImage.image = starbucksAnnotation.image
let button = UIButton(frame: calloutView.starbucksPhone.frame)
button.addTarget(self, action: #selector(ViewController.callPhoneNumber(sender:)), for: .touchUpInside)
calloutView.addSubview(button)
// 3
calloutView.center = CGPoint(x: view.bounds.size.width/2, y: -calloutView.bounds.size.height*0.52)
view.addSubview(calloutView)
mapView.setCenter((view.annotation?.coordinate)!, animated: true)
}