我想将locationManager:didUpdateHeading:
方法返回的不断更新的值存储在 aglobal int
或 aproperty int
中,以便 MotionHandler 类的其他函数可以使用它。但是,此委托方法似乎无法在全局范围内存储其值,而只能在本地存储。这是为什么?是因为它不是实际的 MotionHandler 方法吗?我该如何解决这个问题?谢谢您的帮助。
运动处理器.m
#import "MotionHandler.h"
@interface MotionHandler()
{
CLLocationManager *locationManager;
int degrees; // the global in question..
}
@end
@implementation MotionHandler
-(void) startCompassUpdates
{
locationManager =[[CLLocationManager alloc] init];
locationManager.delegate=self;
[locationManager startUpdatingHeading];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
// This is working, a new value is stored in "degrees" & logged on the console after each update. However it only seems to be updating "degrees" locally..
degrees = (int)locationManager.heading.magneticHeading;
NSLog(@"from delegate method: %i", degrees);
}
-(int) showDegrees
{
return degrees; // This is not working. Whenever I call this method, "degrees" is always zero. Why isn't this global being updated by the previous method ?
}
视图控制器.m
//...
- (void)viewDidLoad
{
[super viewDidLoad];
currentMotionHandler = [[MotionHandler alloc] init];
[currentMotionHandler startCompassUpdates];
while(1==1)
{
NSLog(@"from showDegrees method: %i",[currentMotionHandler showDegrees]); // this just keeps returning zero..
}
}
//...