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时遇到了困难。按钮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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除非原子应该有帮助
removing nonatomic should help