我对 iOS 开发(我的第一个应用程序)还很陌生,我遇到了这个问题。
我有一个 iPhone 应用程序,应该在用户触摸按钮时获取用户在多个 ViewController 中的当前位置。为了防止冗余代码(在不同的 View Controller 中多次实现 locationManager:didFailWithError
、locationManager:didUpdateToLocation:fromLocation
等),我决定创建一个名为 的自定义类位置管理器
:
LocationManager.h
@interface LocationManager : NSObject <CLLocationManagerDelegate> {
@private
CLLocationManager *CLLocationManagerInstance;
id<LocationManagerAssigneeProtocol> assignee;
}
-(void) getUserLocationWithDelegate:(id) delegate;
LocationManager.m
@implementation LocationManager
-(id)init {
self = [super init];
if(self) {
CLLocationManagerInstance = [[CLLocationManager alloc] init];
CLLocationManagerInstance.desiredAccuracy = kCLLocationAccuracyBest;
CLLocationManagerInstance.delegate = self;
}
return self;
}
-(void) getUserLocationWithDelegate:(id) delegate {
if([CLLocationManager locationServicesEnabled]) {
assignee = delegate;
[CLLocationManagerInstance startUpdatingLocation];
}
}
#pragma CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
...
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
[CLLocationManagerInstance stopUpdatingLocation];
[assignee didUpdateToLocation:newLocation];
}
我有一个名为 LocationManagerAssigneeProtocol 的协议(protocol),我的 ViewController 实现了
@protocol LocationManagerAssigneeProtocol <NSObject>
@required
-(void) didUpdateToLocation:(CLLocation *) location;
@end
在我的 View Controller 中需要的地方
- (IBAction)getMyLocation:(id)sender {
[locationMgr getUserLocationWithDelegate:self];
}
这段代码工作得很好,但是,我感觉我在这里违反了一些设计模式,让 LocationManager
能够调用本身发起对位置管理器调用的类的函数。另一方面,我不想为所有应该使用位置的 View Controller 实现 CLLocationManagerDelegate。
有没有更好的解决方案?
最佳答案
我同意@CarlVeazey 的观点。委托(delegate)非常适合随时存在的一对一关系,但是在您的情况下,您似乎可能需要多个 viewController 来在任何给定时间响应位置事件。因此,只需删除与您的委托(delegate)及其相关协议(protocol)相关的任何内容即可。
我可能会将 LocationManager 类设为单例并修改更新方法:
+(LocationManager *)sharedInstance
{
static LocationManager *_sharedInstance = nil;
static dispatch_once_t oncePredicate;
dispatch_once(&oncePredicate, ^{
_sharedInstance = [[self alloc] init];
});
return _sharedInstance;
}
-(void)getUserLocation
{
if ([CLLocationManager locationServicesEnabled])
[CLLocationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[CLLocationManagerInstance stopUpdatingLocation];
[[NSNotificationCenter defaultCenter] postNotificationWithName:@"LocationManagerDidUpdateLocation" object:newLocation];
}
...那么任何需要使用此类的 viewController 都会有类似的内容:
-(void)viewDidLoad
{
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserverForName:@"LocationManagerDidUpdateLocation" object:self queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note) {
CLLocation *location = note.object;
...
}];
}
-(IBAction)getMyLocation:(id)sender {
[[LocationManager sharedInstance] getUserLocation];
}
希望有所帮助并且有意义。
关于iphone - 使用 CLLocationManager 的自定义位置管理器类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14274462/