0

初めての投稿ですが、いつもサイトを読んでいます。複数のクラス間で CLLocation* を渡すことを扱うプログラムに取り組んでいます。それは、CLLocationManager で現在の場所を見つける私のアプリ デリゲートから始まります。場所が見つかると、didUpdateToLocation: newLocation: fromLocation メソッドがこの場所を queryPage という別のクラスに設定します。

//AppDelegate.m
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *)oldlocation {
     currentLocation = newLocation;
     [queryPage setLocation:currentLocation];
}

ここで、場所をログに記録でき、すべて正常に動作します。次に、queryPage で、setLocation が CLLocation を受け取り、それをログに記録できます。問題ありません。ただし、コードの後半では、button1 や button2 などのメソッドで currentLocation にアクセスできません。button1 は時々ログに記録でき、button2 は通常 "(null)" をログに記録します。QueryPage.h は次のとおりです。

//QueryPage.h
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface QueryPage : UIViewController {
     CLLocation *currentLocation;
}
@property (nonatomic, retain) CLLocation *currentLocation;
-(void)setLocation:(CLLocation*)location;

および実装ファイル:

//QueryPage.m
#import QueryPage.h
@implementation QueryPage
@synthesize currentLocation;

...

-(void)setLocation:(CLLocation*)location
{
     self.currentLocation = location;
}

...

-(IBAction)button1:(id)sender
{
     NSLog(@"button1: %@", currentLocation);
}

...

-(IBAction)button2:(id)sender
{
     NSLog(@"button2: %@", currentLocation);
}

currentLocation を間違った方法で設定/保持しているに違いないと思いますが、方法がわかりません。メモリを適切に処理するために何日も検索しましたが、これは私が得た限りです。currentLocation とオンラインで見つけた他の多くのものを保持しようとしました。IB の接続が良好であることはわかっています (タッチするたびにログに記録します)。また、ボタンがタッチされる前に currentLocation が常に設定されます (ログで確認できます)。誰かがこれで私を助けることができるなら、私に知らせてください。これはフォーラムの他の問題と似ていますが、これまでに推奨されたすべてを試しました。ありがとう!

4

1 に答える 1

0

非原子を削除すると役立つはずです

于 2011-04-18T22:14:08.940 に答える