CLLocation iVar释放值,在程序中途变为null

| 这是我的第一篇文章,但我一直都在阅读该网站。我正在研究一个在多个类之间传递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(我可以在日志中看到它)。如果外面有人可以帮助我,请告诉我。这看起来与论坛上的其他问题类似,但是到目前为止,我已经尝试了所有建议的方法。谢谢!
已邀请:
去除非原子应该有帮助

要回复问题请先登录注册