iphone on KVO..How to call he observeValueForKeyPath method from viewWillAppear method?
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.
精彩评论