我使用locationmanger來獲取我在iPhone上的當前位置。objective-c獲取位置並通知更新。觀察者?怎麼樣?
#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訪問它們。當我有座標時,我必須觸發另一個需要座標作爲參數的函數。
在這裏,我的問題開始......
我怎麼現在,當座標檢索和我可以調用它需要的座標作爲輸入參數的其他功能。有沒有一種我可以使用的觀察者?如果,我該如何執行此操作?
我需要的東西,告訴我:嘿老兄,座標可供使用,這樣我就可以觸發下一個功能..
瞭解委託模式http://enroyed.com/ios/delegation-pattern-in-objective-c-and-writing-custom-delegates/。似乎我需要走的路。必須現在用您的建議解決方案進行測試 – jerik