我正在使用CoreLocation,並從我的應用程序AppDelegate中啓動locationManager。示例代碼如下...從其他ViewController的locationManager方法訪問newLocation
AppDelegate.m
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
// ...
// start location manager
if([CLLocationManager locationServicesEnabled])
{
myLocationManager_ = [[CLLocationManager alloc] init];
myLocationManager_.delegate = self;
[myLocationManager_ startUpdatingLocation];
}
else
{
// ... rest of code snipped to keep this short
而且在這個方法中,我們看到更新後的位置。
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSString *currentLatitude = [[NSString alloc] initWithFormat:@"%g", newLocation.coordinate.latitude];
NSLog(@"AppDelegate says: latitude: %@", currentLatitude);
// ... rest of code snipped
現在,我的應用程序的其他領域內,我需要確定用戶當前位置(經度,緯度)。我可以將上面的代碼合併到需要當前位置的ViewControllers中,但是接下來我會運行多個CLLocationManager實例(我認爲) - 以及爲什麼複製此代碼?沒有一種方法可以從其他ViewControllers中獲取AppDelegate的位置信息嗎?
PS - 我使用的Xcode4.3瓦特/ ARC
啊,多美的東西。謝謝! – ElasticThoughts 2012-03-09 19:02:10