Pergunta

Eu tenho um aplicativo com uma barra de abas e 3 guias. A localização atual do usuário vai ser necessário para ser conhecido em qualquer um dos três guias. Seria o melhor lugar para implementar CLLocationManager estar na delegado aplicativo neste caso?

É ok (boas práticas?) Para colocar os métodos CLLocationManager delegado no arquivo aplicativo delegado m?

Onde você sugere que eu coloque o CLLocationManager como eu vou estar chamando -startUpdatingLocation de qualquer um dos três guias?

Graças

Foi útil?

Solução

O delegado aplicativo é um lugar razoável para colocá-lo. Outra opção seria a criação de uma classe de fábrica Singleton personalizado que tem um método de classe que retorna seu gerente delegado localização e implementar os métodos de delegado lá. Que iria manter seu aplicativo classe delegado mais limpo.

Aqui está uma IMPLEMENTAÇÃO esqueleto única classe baseado fora de Peter Hosey do " singletons em Cocoa: fazê-las errado ". Este pode ser um exagero, mas é um começo. Adicione seus métodos de delegado no 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...

Outras dicas

Eu simplesmente incluí meu LocationManager na minha AppDelegate diretamente, uma vez que acrescentou pouco código.

No entanto, se você estiver indo para incluir seu LocationManager na AppDelegate, então você deve considerar o uso NSNotifications para alertar seus viewcontrollers do local atualiza o seu AppDelegate recebe.

Veja este link Enviar e receber mensagens através NSNotificationCenter em Objective-C?

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top