gpt4 book ai didi

core-location - 在哪里实现 CLLocationManager

转载 作者:行者123 更新时间:2023-12-04 12:15:01 24 4
gpt4 key购买 nike

我有一个带有标签栏和 3 个标签的应用程序。需要在三个选项卡中的任何一个上知道用户的当前位置。最好的实现地点CLLocationManager在这种情况下是在应用程序委托(delegate)中吗?

将 CLLocationManager 委托(delegate)方法放在应用程序委托(delegate) m 文件中是否可以(好的做法?)?

您建议我将 CLLocationManager 放在哪里?因为我要调用-startUpdatingLocation从三个选项卡中的任何一个?

谢谢

最佳答案

应用程序委托(delegate)是放置它的合理位置。另一种选择是创建一个自定义单例工厂类,该工厂类具有一个类方法,该类方法返回您的位置管理器委托(delegate)并在那里实现委托(delegate)方法。这将使您的应用程序委托(delegate)类更清洁。

这是一个基于 Peter Hosey 的 "Singletons in Cocoa: Doing them wrong" 的骨架单例类实现。 .这可能有点矫枉过正,但这是一个开始。最后添加您的委托(delegate)方法。

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...

关于core-location - 在哪里实现 CLLocationManager,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1862304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com