How to set the .xib interface as a custom callout view?

I just created CustomCallout.xib with a GUI. I would like to use it as a custom callout for my annotations, but I don’t know where I should set the displayed view and how to pass values ​​to this view.

The view of the leader that I would like to display is as follows.enter image description here

Here is the code for my custom annotation .

class MapPin : MKPointAnnotation {
var name: String
var street: String
var type: String
var postCode: String

init(name: String, street: String, type: String, postCode: String) {
    self.name = name
    self.street = street
    self.type = type
    self.postCode = postCode
}}

And my view controller :

import UIKit
import MapKit

class ViewController: UIViewController, MKMapViewDelegate {

@IBOutlet var mapView: MKMapView!
let span = MKCoordinateSpanMake(0.05, 0.05)


override func viewDidLoad() {
    super.viewDidLoad()

    self.mapView.delegate = self

    let location = CLLocationCoordinate2D(
        latitude: 53.430,
        longitude: 14.529)

    let region = MKCoordinateRegion(center: location, span: span)
    mapView.setRegion(region, animated: true)

    let punkt = MapPin(name: "rand", street: "rand", type: "rand", postCode: "rand")
    punkt.coordinate = location

    mapView.addAnnotation(punkt)
}

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKAnnotationView!) {
    if view.annotation.isKindOfClass(MKUserLocation){
        return
    }

    var customView = (NSBundle.mainBundle().loadNibNamed("CustomCallout", owner: self, options: nil))[0] as! CustomCallout;

    var calloutViewFrame = customView.frame;
    calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
    customView.frame = calloutViewFrame;

    let cpa = view.annotation as! MapPin

    view.addSubview(customView)

    var newRegion = MKCoordinateRegion(center:cpa.coordinate, span: span)
    self.mapView.setRegion(newRegion, animated: true)
}}

Unfortunately, the running application shows correctly generated output with the correct location, but the values ​​from are let punkt = MapPin(name: "rand", street: "rand", type: "rand", postCode: "rand") not passed to the view, so it looks like this.enter image description here

+4
1

, Swift.

func mapView(mapView: MKMapView!, didSelectAnnotationView view: MKPinAnnotationView!)
{
    if view.annotation.isKindOfClass(MKUserLocation){
        return
    }

    var customView = (NSBundle.mainBundle().loadNibNamed("SubView", owner: self, options: nil))[0] as CustomSubView;

    var calloutViewFrame = customView.frame;
    calloutViewFrame.origin = CGPointMake(-calloutViewFrame.size.width/2 + 15, -calloutViewFrame.size.height);
    customView.frame = calloutViewFrame;

    let cpa = view.annotation as CustomPointAnnotation
   //You can add set vlaues for  here
   //cpa.title
   //cpa.postCode
   //cpa.street

    view.addSubview(customView)

    //zoom map to show callout
    let spanX = 0.0000000000000001
    let spanY = 0.0000000000000001

    var newRegion = MKCoordinateRegion(center:cpa.coordinate, span: MKCoordinateSpanMake(spanX, spanY))
    self.map?.setRegion(newRegion, animated: true)
   }
+8

Source: https://habr.com/ru/post/1584007/


All Articles