0

我使用 locationmanger 来获取我在 iPhone 上的当前位置。

#pragma mark - Location handling
-(void)doLocation {
    self.locationManager = [[CLLocationManager alloc] init];
    [...]
    SingletonClass *sharedSingleton = [SingletonClass sharedInstance];
    sharedSingleton.coordinate = [location coordinate];
}

- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation
{
    [...]
    SingletonClass *sharedSingleton = [SingletonClass sharedInstance];
    [sharedSingleton setCoordinate:CLLocationCoordinate2DMake(currentLat, currentLng)];

    NSLog(@"debug: self.currentLoc: %@", self.currentLoc);

}

这很好用,我得到了一些延迟的位置坐标,并且可以通过 sharedSingleton 访问它们。当我有坐标时,我必须触发另一个需要坐标作为参数的函数。

我的问题从这里开始......

我现在该怎么办,当检索到坐标时,我可以调用需要坐标作为输入参数的其他函数。有没有我可以使用的观察者?如果,我该如何实施?

我只需要一些告诉我的东西:嘿,伙计,坐标可以使用了,这样我就可以触发下一个功能了..

4

2 回答 2

1

您可以使用NSNotificationCenter或 委托。目标 C 中的委托模式非常普遍。为此,您需要在 .h 文件中实现一个协议,如下所示:

@protocol MyLocationManagerDelegate <NSObject>
- (void) locationManager:(MyLocationManager *)manager didFindLocation:(CCLocation*) location
@end

//still in your .h file add a delegate that implements this protocol
@property (nonatomic, assign) id<MyLocationManagerDelegate> delegate

然后在找到坐标时必须采取行动的其他类中,表明它实现了MyLocationManagerDelegate协议,并实现了该- (void) locationManager:(MyLocationManager *)manager didFindLocation:(CCLocation*) location方法。

分配您的位置经理后,将您的其他班级设置为代表。在您的didUpdateToLocation方法中只需调用[self.delegate locationManager:self didFindLocation:self.currentLoc]

于 2013-07-02T20:57:29.947 回答
0

实现此功能的一种方法是 NSNotificationCenter。

这篇文章将向您展示如何通过 NSNotificationCenter 设置通知。

Objective-c中通过NSNotificationCenter发送和接收消息

于 2013-07-02T20:53:44.390 回答