El delegado de la aplicación es un lugar razonable para decirlo. Otra opción sería crear una clase personalizada de fábrica de singleton que tenga un método de clase que devuelva su delegado de administrador de ubicación e implemente allí los métodos de delegado. Eso mantendría limpia la clase de delegados de tu aplicación.
Aquí hay una clase Singleton IMPLEMENTACIÓN esqueleto con sede fuera de Peter Hosey de "Singletons in Cocoa: Doing them wrong". Esto puede ser excesivo, pero es un comienzo. Agregue sus métodos de delegado al final.
static MyCLLocationManagerDelegate *sharedInstance = nil;
+ (void)initialize {
if (sharedInstance == nil)
sharedInstance = [[self alloc] init];
}
+ (id)sharedMyCLLocationManagerDelegate {
//Already set by +initialize.
return sharedInstance;
}
+ (id)allocWithZone:(NSZone*)zone {
//Usually already set by +initialize.
@synchronized(self) {
if (sharedInstance) {
//The caller expects to receive a new object, so implicitly retain it
//to balance out the eventual release message.
return [sharedInstance retain];
} else {
//When not already set, +initialize is our caller.
//It's creating the shared instance, let this go through.
return [super allocWithZone:zone];
}
}
}
- (id)init {
//If sharedInstance is nil, +initialize is our caller, so initialze the instance.
//If it is not nil, simply return the instance without re-initializing it.
if (sharedInstance == nil) {
if ((self = [super init])) {
//Initialize the instance here.
}
}
return self;
}
- (id)copyWithZone:(NSZone*)zone {
return self;
}
- (id)retain {
return self;
}
- (unsigned)retainCount {
return UINT_MAX; // denotes an object that cannot be released
}
- (void)release {
// do nothing
}
- (id)autorelease {
return self;
}
#pragma mark -
#pragma mark CLLLocationManagerDelegateMethods go here...
¿Cómo hago para implementar una clase de fábrica Singleton? Eso suena como una forma limpia de hacerlo, pero no estoy 100% seguro de por dónde empezar. – joec
Vea el ejemplo anterior. –
de formato de código no funcionó (lo siento!) – joec