2017-01-12 2 views
0

私は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]; 
} 
+0

似たようなデバイスを使用しましたか? 'crnLoc'は' nil'と同じですか? –

+0

はい私はそれを実際のデバイスで使用しました。実際にはcrnLocはゼロではありません。私は自分のコンソールに私の経度と緯度を表示していましたが、内部のdidUpdateLocationsしか動作しませんでした –

答えて

0

ラベルのテキストは、didLoadメソッドで一度だけ設定されていますが、場所の更新後は更新されません。オブザーバーを使用する場合は、gpsModelインスタンスと緯度/経度プロパティーを観察する必要があります。 observeValueForKeyPathデリゲートメソッドでは、テキストラベルを更新する必要があります。

+0

ラベル内のテキストを観察する前に、ラベルに経度を表示できませんでした。私はモデル –

+0

からnullを取得しています。ビューコントローラのラベルはどこで更新できますか? –

+0

CoreLocationが最初のロケーション更新を受け取る場合にのみ、正しい値を表示することができます。その前に、正しい場所がヌルになります。 –

関連する問題