简体   繁体   English

watchValueForKeyPath不起作用

[英]observeValueForKeyPath doesn't work

This my first time using KVO in an iOS project and I wanted to observe the changes of my two UILabel's after getting my position by GPS, and keep notifying me. 这是我第一次在iOS项目中使用KVO,我想通过GPS定位后观察两个UILabel的变化,并不断通知我。 when I run the project I got my 2 labels showing the date and they are changing by time, but observeValueForKeyPath doesn't work. 当我运行项目时,我得到了2个标签,它们显示了日期,并且它们都随着时间而变化,但是observeValueForKeyPath不起作用。

@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:@"long" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
    gpsView.latitudeLabel.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.latitude];
    [gpsView.latitudeLabel addObserver:self forKeyPath:@"lat" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];
 }

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if([keyPath isEqualToString:@"long"]) {
        NSLog(@"observe1");
    }
    if([keyPath isEqualToString:@"lat"]) {
        NSLog(@"observe2");
    }
}

It should be more like this: 应该更像这样:

[gpsView.longitudeLabel addObserver:self forKeyPath:@"text" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld context:nil];

and in 和在

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context;

your object is the corresponding label, so you could check for 您的对象是相应的标签,因此您可以检查

if object == gpsView.longitudeLabel

You can't just create any string inside forKeyPath . 您不能只在forKeyPath内部创建任何字符串。 This determines the name of variable you are observing, so you can use for example "text" as forKeyPath if you want to observe changes in text. 这确定了要观察的变量的名称,因此,如果要观察文本的变化,可以将“ text”用作forKeyPath

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM