【发布时间】:2014-01-11 22:19:19
【问题描述】:
这是一个定位应用。当我第一次在 iPhone 上运行该应用程序时,会出现一个警告窗口,询问我是否要允许定位服务。选择“确定”或“不允许”后,我可以点击一个设置为显示我当前位置的按钮。该按钮还设置为使用名为“locationServicesEnabled”的 BOOL 函数的值将 NSLog 打印到控制台,如果是则返回 yes/1,如果不是则返回 no/0。
我的问题是,当我选择不允许选项并按下按钮时,NSLog 仍然将值 1 打印到控制台,这是不正确的。它应该为 0 或 false。连接到我的按钮的文本标签甚至显示“您的位置为空”,但由于某种原因,NSLog 始终显示 BOOL 值 1。
这是我的 ViewController.m 代码:
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
self.gpsLM = [[CLLocationManager alloc]init];
[self.gpsLM startUpdatingLocation];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{
}
-(IBAction)gpsButton{
CLLocation * currentLocation = self.gpsLM.location;
self.gpsLabel.text = [NSString stringWithFormat:@"Your Location is %@", currentLocation];
NSLog(@"Location services enabled: %hhd",[CLLocationManager locationServicesEnabled]);
}
@end
【问题讨论】:
标签: ios objective-c boolean cllocationmanager nslog