在KVO上的iphone如何从viewWillAppear方法调用他的observeValueForKeyPath方法?

时间:2011-08-17 21:19:36

标签: iphone key-value-observing

以下是我的东西......

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([object isKindOfClass:[UIAccelerometer class]]) {   
    NSNumber *interfaceValue = [change objectForKey:NSKeyValueChangeNewKey];
    UIInterfaceOrientation toInterfaceOrientation = [interfaceValueintValue];           
    //here is my code....   
    }

1 个答案:

答案 0 :(得分:1)

如果我理解正确,您希望在keyValue更改时执行某些代码 - 并且您还希望在viewWillAppear中执行此代码。而不是尝试以编程方式触发KVO方法,只需创建一个可以从两个位置调用的单独函数:

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    [self myKeyValueObservationMethod];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self myKeyValueObservationMethod];
}

- (void)myKeyValueObservationMethod {
    // here is my code....
}

如果我完全错过了商标,请编辑您的问题,并在您的问题解释中添加更多详细信息。