1

appDelegate に、緯度と経度を取得し、任意の viewController で使用できる文字列を返すメソッドがあります。

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];  

しかし、私はクラッシュします

str = [str stringByAppendingString:strLatitude];  

returnLatLongString

その時点でstrLatitudeに値がないため、クラッシュしていることはわかっています。しかし、どうすればこれを修正できますか? 緯度と経度の値を更新するにはどうすればよいですか?

また、 viewControllers でlocationManagerを使用したくありません。すべてのviewControllerで現在の場所を取得できるように、appDelegateで行いました。

4

5 に答える 5

5

クラッシュはstrNSStringが原因である可能性があるため、変更できません。この場合、それをそれ自体に割り当てることは問題があります。使用するだけの方が簡単ですstringWithFormat

NSString *str = [NSString stringWithFormat: @"lat=%@&long=%@", strLatitude, strLongitude];
于 2012-04-17T12:41:23.000 に答える
0

AppDelegateクラスのオブジェクトを作成する際に間違いを犯しています..

オブジェクトを作成するには、次のコードを記述します。

AppDelegate *appDelegate=(AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *str = [appDelegate returnLatLongString];

終わった..

于 2012-04-17T12:38:43.540 に答える
0

AppDelegate.h ファイルをインポートします。次に
、次のコードを使用します。

MyAppDelegate *myAppDelegate = (MyAppDelegate *)[[UIApplication sharedApplication] delegate];
NSString *result = [myAppDelegate returnLatLongString];
于 2012-04-17T12:40:10.030 に答える
0

メソッドでこれらの変数(strLatitude、strLongitude)を初期化する必要があると思います - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions。最初にこの時点に達すると、初期化されないためクラッシュします。

于 2012-04-17T14:55:32.383 に答える
0

コードには 2 つの潜在的な問題があります。

まず、ヘッダー ファイルでstrLatitudeandstrLongitudeをプロパティとして宣言していることを確認してください。strong

@property (nonatomic, strong) NSString * strLatitude;    
@property (nonatomic, strong) NSString * strLongitude;

@synthesizeそれらに適切なゲッターとセッターを自動的に生成し、適切な値を次のように割り当てるために使用します。

float latitude = newLocation.coordinate.latitude;
self.strLatitude = [NSString stringWithFormat:@"%f",latitude];
float longitude = newLocation.coordinate.longitude;
self.strLongitude = [NSString stringWithFormat:@"%f", longitude];

[NSString stringWithFormat:]オブジェクトを返すため、値が割り当てられた後に解放されないようにしautoreleasedます。

2 つ目は[locManager startUpdatingLocation];、システムが最初の位置情報の更新を配信するまでに時間がかかることです。strLatitudeしたがって、とに値が割り当てられているかどうかを確認してから、strLongitude別の文字列を作成する必要があります。これに似たものが仕事をするはずです:

-(NSString*)returnLatLongString
{
    if (self.strLatitude == nil || self.strLongitude == nil)
        return nil;

    NSString *str = @"lat=";
    str = [str stringByAppendingString:strLatitude];
    str = [str stringByAppendingString:@"&long="];
    str = [str stringByAppendingString:strLongitude];

    return str;
}

もちろん、これから使用するView Controllerは[AppDelegate returnLatLongString]、返された値が ではないことを確認する必要がありnilます。

お役に立てれば...

于 2012-04-17T15:08:41.720 に答える