agillespie agillespie - 6 months ago 63
iOS Question

How to set singleton class of MKMapView as the map in Storyboard?

I created a singleton class of

MKMapView
that I use across many classes to turn on and off
showsUserLocation
and tracking methods. The code seems to work on the singleton class; however, it has no effect on the actual map in the simulator. I believe that the
MKMapView
in the
Storyboard
is not actually the instance of the Singleton class, even though I changed its class in the Interface Builder Custom Class category, and created an
IBOutlet
to it. How can I make sure the map in the
Storyboard
is the same map that my code is affecting?

Answer

You could override the initWithCoder: method. This is the method that is called to unarchive a class from the storyboard.

static MyMapView *map = nil;

- (instancetype)initWithCoder:(NSCoder *)coder
{
    if (map) {
        return map;
    }
    else {
        @synchronized (self) {
            if (!map) {
                map = [super initWithCoder:coder];
            }
            return map;
        }
    }
}

Note: I have never implemented this so it may need tweaking.