1

I want to observe let's say the bounds on the view of the viewController

[self.view addObserver:self forKeyPath:@"bounds" options:0 context:nil];

but how to unregister it in dealloc, when the self.view is already nil?

Note, that this question is not a duplicate "KVO and ARC how to removeObserver" as the answer doesn't handle this problem

Community
  • 1
  • 1
Peter Lapisu
  • 19,915
  • 16
  • 123
  • 179
  • according to the `UIViewController` life cycle and _state restoration and preservation_ policy (https://developer.apple.com/library/ios/documentation/uikit/reference/UIViewController_Class/index.html) you could add an observer in the `–viewWillAppear:` method and you'd need to remove that in the `–viewDidDisappear:` method. – holex Oct 06 '14 at 15:17
  • allright, but this may concernt the NSViewController, which doesn't have viewWillAppear: viewDidDisappear: – Peter Lapisu Oct 06 '14 at 15:18
  • probably I don't understand your problem then... regarding the observers are always _weak_ references in `ARC`, you should not worry about removing them before the observed object is released permanently. which means, when your `self.view` is `nil`, the observer has removed implicitly already. can you please clarify what your goal is here actually? – holex Oct 06 '14 at 15:22
  • 1
    the problem is, that KVO doesn't use ARC and keeps an unsafe reference... so it needs to be explicitly removed – Peter Lapisu Oct 06 '14 at 16:59

0 Answers0