私はiOSプロジェクトを作成しました。didUpdateLocations
を実装して、私の所在地をNSObject
にしました。デリゲートから経度と緯度をUIViewController
に取得したいのですが、結果は常にaddObserver
をテストするためにもnullです。一番下には私がしたサンプルがあり、ラベルに自分の位置が示されています。私はdidUpdateLocations
をviewcontrollerに実装しました。NSObject
クラスはありません。NSObjectのdidUpdateLocationsのNSStringをUIViewControllerに渡します。
NSObject
でこの私のコード:
#import "GpsModel.h"
@implementation GpsModel{
CLLocation *crnLoc;
}
@synthesize locationManager;
@synthesize longitude;
@synthesize latitude;
- (void) initLocation {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
//[self showAlert];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
crnLoc = [locations lastObject];
longitude = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude];
latitude = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude];
}
@end
、これが私のViewControllerです:
#import "GpsViewController.h"
@interface GpsViewController()
@end
@implementation GpsViewController{
CLLocationManager *locationManager;
}
@synthesize gpsView;
@synthesize gpsModel;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
gpsView = [[GpsView alloc] initWithFrame:CGRectMake(0, 0, widthtScreen, heightScreen)];
[self.view addSubview:gpsView];
gpsModel = [GpsModel new];
[gpsModel initLocation];
gpsView.longitudeLabel.text = gpsModel.longitude;
[gpsView.longitudeLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
gpsView.latitudeLabel.text = gpsModel.latitude;
[gpsView.latitudeLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(object == gpsView.longitudeLabel)
{
NSLog(@"long");
}
if(object == gpsView.latitudeLabel)
{
NSLog(@"lat");
}
}
@end
そしてNSObjectのクラスを作成せずに、この私のコード:
@interface GpsViewController()
@end
@implementation GpsViewController{
CLLocationManager *locationManager;
CLLocation *crnLoc;
}
@synthesize gpsView;
@synthesize gpsModel;
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
gpsView = [[GpsView alloc] initWithFrame:CGRectMake(0, 0, widthtScreen, heightScreen)];
[self.view addSubview:gpsView];
[self initLocation];
}
- (void) initLocation {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
[self showAlert];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
crnLoc = [locations lastObject];
NSLog(@"position long : %@",[NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude]);
NSLog(@"position lat : %@",[NSString stringWithFormat:@"%.8f",crnLoc.coordinate.latitude]);
gpsView.longitudeLabel.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude];
[gpsView.longitudeLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
gpsView.latitudeLabel.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.latitude];
[gpsView.latitudeLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if(object == gpsView.longitudeLabel)
{
NSLog(@"observe1");
}
if(object == gpsView.latitudeLabel)
{
NSLog(@"observe2");
}
}
これは2つのラベルを含む私の見解です
@implementation GpsView{
CGFloat _originY;
}
@synthesize longitudeLabel;
@synthesize latitudeLabel;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
_originY = 50;
[self _initView];
}
return self;
}
- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self _initView];
}
return self;
}
- (void)_initComponents {
longitudeLabel = [[UILabel alloc] init];
latitudeLabel = [[UILabel alloc] init];
}
- (void)_initView {
[self _initComponents];
[self _createComponentView];
}
-(void) _createComponentView {
_originY = [self createLabel:_originY :longitudeLabel :self];
[self addSubview:longitudeLabel];
_originY = [self createLabel:_originY :latitudeLabel :self];
[self addSubview:latitudeLabel];
}
似たようなデバイスを使用しましたか? 'crnLoc'は' nil'と同じですか? –
はい私はそれを実際のデバイスで使用しました。実際にはcrnLocはゼロではありません。私は自分のコンソールに私の経度と緯度を表示していましたが、内部のdidUpdateLocationsしか動作しませんでした –