2011-08-17 24 views
0

以下是我的東西....iPhone上KVO ..如何從viewWillAppear方法調用他觀察ValueForKeyPath方法?

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

你有什麼問題? – jtbandes

回答

1

如果我理解正確的話,你想執行某些代碼時,密鑰值的變化 - 而你也想在viewWillAppear中執行該代碼。而不是試圖以編程方式觸發志願方法,只需創建一個單獨的功能,可以從兩個位置撥打:

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

如果我完全錯過了標記,那麼請編輯你的問題,並增加更多的細節,你的解釋你的問題。

+0

是的,我做了同樣的工作,通過在viewWillAppear()中放置相同的代碼... –