-2

appDelegate を使用して、別のビューからユーザーの場所を取得しようとしています。NSUserDefaults に保存することもできましたが、それを行うためのより良い方法があるかどうか疑問に思っていました。

私のAppDelegateにはこれが含まれています:

- (NSString *)deviceLocation
{
NSString *userCoordinates = [NSString stringWithFormat:@"latitude: %f longitude: %f", 
locationManager.location.coordinate.latitude, 
locationManager.location.coordinate.longitude];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];

return userCoordinates;

}

次に、これを使用して別のページからユーザーの場所を呼び出します。

//Get the User's Location
NSString *location =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate deviceLocation];
4

1 に答える 1

1

AppDelegate の deviceLocation メソッドが何も返さないため、これは機能しません。

現在の場所を警告したい場合は、deviceLocation が何かを返す必要があります (つまり、現在の場所を含む NSString)。

次に、このようなもので表示します

NSString *location =[(PDCAppDelegate *)[UIApplication sharedApplication].delegate deviceLocation];

//Alert
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Your Location" 
message:location delegate:self cancelButtonTitle:@"Dismiss" otherButtonTitles:nil, 
nil];
[alert show];
于 2012-12-16T00:32:55.893 に答える