CLLocation iVar 丢失值,在程序中途变为 null

发布于 2024-11-02 07:24:45 字数 1567 浏览 0 评论 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时遇到了困难。按钮1有时会记录它,按钮2通常记录“(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 始终在触摸任何按钮之前设置(我可以在日志中看到它)。如果有人可以帮助我解决这个问题,请告诉我。这看起来与论坛上的其他一些问题类似,但我已经尝试了迄今为止推荐的所有内容。谢谢!

this is my first post, but I read the site all the time. I am working on a program that deals with passing a CLLocation* between multiple classes. It begins with my app delegate finding the current location with the CLLocationManager. Once the location is found, the didUpdateToLocation: newLocation: fromLocation method sets this location to another class called queryPage:

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

Here, I can log the location, and everything works fine. Then, in queryPage, setLocation can receive the CLLocation and log it, no problem. However, later in my code, methods like button1 and button2 have trouble accessing currentLocation. button1 can log it sometimes, and button2 usually logs "(null)". Here is 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;

and the implenentation file:

//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);
}

I think that I must be setting/retaining currentLocation the wrong way, but I have no idea how. I have searched for days trying to handle the memory properly and this is as far as I have gotten. I tried retaining currentLocation and many other things I could find online. I know that the connections in IB are good (I log it each time they are touched), and currentLocation is always set before any buttons are touched (I can see it in the log). If anyone out there can help me with this, please let me know. This looks similar to some other problems on the forum, but I have tried everything that has been recommended so far. Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七色彩虹 2024-11-09 07:24:45

删除非原子应该有帮助

removing nonatomic should help

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文