2012-04-20 55 views
1

我有一個MainViewController與NSString屬性currentTag。所以我這樣做:KVO在外部類不工作

[self addObserver:self forKeyPath:@"currentTag" options:NSKeyValueObservingOptionNew context:nil]; 

這工作正常,並且更改得到正確處理。不過,我有另一個類SecondViewController,我也希望它遵守MainViewController的currentTag,所以在SecondViewController的viewDidLoad方法,我做的:

[self addObserver:self.mainViewController forKeyPath:@"currentTag" options:NSKeyValueObservingOptionNew context:nil]; 

然而,這一次沒有得到某種原因處理。它永遠不會被調用。我想確保我的語法和形式是正確的,然後再查看我的代碼中的其他問題。這段代碼看起來好嗎?

回答

6

你就錯了,你應該做的:

[self.mainViewController addObserver:self forKeyPath:@"currentTag" options:NSKeyValueObservingOptionNew context:nil];