我正在尝试创建一个应用,它会在我在故事板中设置的标签中收集并显示我的经度和纬度值。如果我只是在我的IBAction实例中为我的标签分配一个简单的文本,它运行正常并显示文本。但是,当我在下面的IBAction实例中使用代码时,按下我的按钮时,我的标签中没有显示任何内容。
@interface ViewController ()
@end
@implementation ViewController {
CLLocationManager *locationManager;
}
@synthesize longitude, latitude;
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)getCurrentLocation:(id)sender {
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc]
initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"didUpdateToLocation: %@", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
latitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}
}
@end