我有一個方法在我的appDelegate獲取經緯度並返回一個字符串,我可以在任何viewController中使用。從AppDelegate獲取當前位置
的AppDelegate:
-(void)StartUpdating
{
locManager = [[CLLocationManager alloc] init];
locManager.delegate = self;
locManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locManager startUpdatingLocation];
}
#pragma mark
#pragma mark locationManager delegate methods
- (void)locationManager: (CLLocationManager *)manager
didUpdateToLocation: (CLLocation *)newLocation
fromLocation: (CLLocation *)oldLocation
{
float latitude = newLocation.coordinate.latitude;
strLatitude = [NSString stringWithFormat:@"%f",latitude];
float longitude = newLocation.coordinate.longitude;
strLongitude = [NSString stringWithFormat:@"%f", longitude];
//[self returnLatLongString:strLatitude:strLongitude];
}
-(NSString*)returnLatLongString
{
NSString *str = @"lat=";
str = [str stringByAppendingString:strLatitude];
str = [str stringByAppendingString:@"&long="];
str = [str stringByAppendingString:strLongitude];
return str;
}
我打電話StartUpdating在應用程序的啓動。現在,在我的viewController我調用這個方法:
AppDelegate *appDelegate=[AppDelegate sharedAppDelegate];
NSString *str = [appDelegate returnLatLongString];
但我在returnLatLongString
得到一個崩潰的
str = [str stringByAppendingString:strLatitude];
。
我知道我正在崩潰,因爲當時strlatitude沒有任何價值。但我該如何解決這個問題?我怎樣才能更新緯度和經度的值?我不想在viewControllers中使用locationManager。爲了能夠在所有viewController中獲取當前位置,我在appDelegate中完成了它。
在'AppDelegate中*的appDelegate = [AppDelegate中sharedAppDelegate]'我覺得作者做了正確的事情:從sharedApplication返回委託像'回報(AppDelegate中*)[[UIApplication的sharedApplication]委託];'。 – 2015-04-21 07:25:49