Ios 在iphone 6.1模拟器上获取位置时遇到问题
我对objective C和Xcode非常陌生。我试图找到我的iphone模拟器的位置,并在youtube上浏览了一个教程。下面是代码Ios 在iphone 6.1模拟器上获取位置时遇到问题,ios,objective-c,iphone,xcode,Ios,Objective C,Iphone,Xcode,我对objective C和Xcode非常陌生。我试图找到我的iphone模拟器的位置,并在youtube上浏览了一个教程。下面是代码 #import "MainClassViewController.h" #import <CoreLocation/CoreLocation.h> @interface MainClassViewController () <CLLocationManagerDelegate> @end @implementation MainCl
#import "MainClassViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface MainClassViewController () <CLLocationManagerDelegate>
@end
@implementation MainClassViewController
{
CLLocationManager *manager;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
manager = [[CLLocationManager alloc]init];
}
- (IBAction)LocationButton:(id)sender
{
manager.delegate = self;
manager.desiredAccuracy = kCLLocationAccuracyBest;
[manager startUpdatingLocation];
}
#pragma mark CLLocationManagerDelegate Methods
- (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 didUpdateLocations:(CLLocation *)newLocation fromLocation: (CLLocation *) oldLocation {
NSLog(@"Location: %@", newLocation);
CLLocation *currentLocation = newLocation;
if(currentLocation != nil){
_Lattitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
_Longitude.text = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
}
}
#导入“MainClassViewController.h”
#进口
@接口MainClassViewController()
@结束
@实现MainClassViewController
{
CLLocationManager*经理;
}
-(无效)viewDidLoad
{
[超级视图下载];
//加载视图后,通常从nib执行任何其他设置。
manager=[[CLLocationManager alloc]init];
}
-(iAction)位置按钮:(id)发送器
{
manager.delegate=self;
manager.desiredAccuracy=KCallocationAccuracyBest;
[经理startUpdatingLocation];
}
#pragma mark CLLocationManagerDelegate方法
-(无效)locationManager:(CLLocationManager*)manager错误:(N错误*)错误
{
NSLog(@“错误:%@”,错误);
UIAlertView*errorAlert=[[UIAlertView alloc]
initWithTitle:@“错误”消息:@“无法获取您的位置”委托:nil cancelButtonTitle:@“确定”其他ButtonTitles:nil];
[错误警报显示];
}
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(CLLocation*)newLocation fromLocation:(CLLocation*)oldLocation{
NSLog(@“位置:%@”,新位置);
CLLocation*currentLocation=newLocation;
如果(当前位置!=nil){
_latitude.text=[NSString stringWithFormat:@“%.8f”,currentLocation.coordinate.latitude];
_Longitude.text=[NSString stringWithFormat:@“%.8f”,currentLocation.coordinate.Longitude];
}
}
@结束
#导入
@接口MainClassViewController:UIViewController
-(iAction)位置按钮:(id)发送器;
@性质(弱,非原子)IBUILabel*格度;
@属性(弱,非原子)IBUILabel*经度;
@结束
我的问题是:
1.我不知道为什么标签没有更新位置。
2.我想了解有关调试的更多信息。任何可用的适当资源。请记住,我不想了解有关调试的所有信息。只有基本要求。您在模拟器中测试了吗?是的。当我点击按钮时,它应该将标签中的文本更改为纬度和经度。下面是头文件的代码:那么您是否在inteface builder中附加了按钮的操作和标签插座?对于调试,至少NSLog应该可以帮助您。您在控制台中看到任何输出吗?是的,我在interface builder中附加了按钮的操作和标签插座。控制台中没有输出。
#import <UIKit/UIKit.h>
@interface MainClassViewController : UIViewController
- (IBAction)LocationButton:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *Lattitude;
@property (weak, nonatomic) IBOutlet UILabel *Longitude;
@end