开发者

iphone on KVO..How to call he observeValueForKeyPath method from viewWillAppear method?

开发者 https://www.devze.com 2023-03-28 16:21 出处:网络
The following is my stuff.... - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {

The following is my stuff....

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([object isKindOfClass:[UIAccelerometer class]]) {   
    NSNumber *interfaceValue = [change ob开发者_StackOverflow中文版jectForKey:NSKeyValueChangeNewKey];
    UIInterfaceOrientation toInterfaceOrientation = [interfaceValueintValue];           
    //here is my code....   
    }


If I understand you correctly, you would like to execute certain code when a keyValue changes - and you would also like to execute this code in viewWillAppear. Rather than trying to programmatically trigger the KVO method, simply create a separate function that you can call from both locations:

- (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....
}

If I am completely missing the mark, then please edit your question and add more detail to your explanation of your problem.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号