kuzdu
kuzdu

Reputation: 7524

Delegate: MKMapView is nil, though it is initialized

When I receive in my Bluetooth class (new) values from my device, then I call a delegate. So the Bluetooth class is running in the background.

My protocol is simple:

protocol RefreshPositionInDrive {
func changingValue(latitude: Double, longitude: Double)
}

In my UIViewController I initialize a map. When I worked at the beginning without delegates this code works fine.

func initializeMapResolution() {
    let regionRadius: CLLocationDistance = 1000
    let initialLocation = CLLocation(latitude: 50.910349, longitude: 8.066895)
    let coordinateRegion = MKCoordinateRegionMakeWithDistance(initialLocation.coordinate,
    regionRadius * 1.5, regionRadius * 1.5)
    MapDrive.setRegion(coordinateRegion, animated: true)
    MapDrive.delegate = self
}

My method from my protocol:

func changingValue(latitude: Double,longitude: Double) {
    print("THE NEW COORDINATES \(latitude)  \(longitude)")

    if self.MapDrive == nil {
       print("Is nil")
    } else {
        updateTheMap()
    }
}

Output:

THE NEW COORDINATES 25.012x 16.992

Is nil

But I don't understand that. I initialize my map first. After that the changingValue is called. How can be the MapDrive nil? I tested the code without delegates, just with some fix coordinates in my UIViewController and the annotation appears.

(I'm working the first time with delegates.)

EDIT

I was indistinctly: My MapDrive:

 @IBOutlet weak var MapDrive: MKMapView!

So I can't instantiate like you mean or?

Upvotes: 0

Views: 463

Answers (1)

Fred Faust
Fred Faust

Reputation: 6800

You'll want to reference a MapDrive instance to the UIViewController, your MapDrive is probably released when your function ends.

class UIViewController {

var mapDrive = MapDrive()

func initializeMapResolution() {

     // Instantiate map drive, assign it to self.mapDrive
     //then assign the delegate of the property on self.      
     self.mapDrive.delegate = self

}

}

Upvotes: 1

Related Questions