title: Why is this code not KVO-Compliant?
body: <p>I have a <code>NSViewController</code> subclass with a convenience method to return the window controller like this</p>
  
  <pre><code>@interface ILViewController : NSViewController
  
  - (ILWindowController *)windowController;
  
  @end
  
  @implementation ILViewController
  
  - (NSWindowController *)windowController {
      return self.view.window.windowController;
  }
  
  - (void)setView:(NSView *)view {
      [self willChangeValueForKey:@"windowController"];
      [super setView:view];
      [self didChangeValueForKey:@"windowController"];
  }
  
  @end
  </code></pre>
  
  <p>The window controller has a <code>NSString *</code> property named <code>mainStatus</code> which I'm trying to bind to through the view controller. However, I am getting this error at run time. </p>
  
  <pre><code>Cannot update for observer &lt;NSAutounbinderObservance 0x1005cf990&gt; for the key path 
  "windowController.mainStatus" from &lt;ILViewController 0x1001976b0&gt;, most likely because
  the value for the key "windowController" has changed without an appropriate KVO
  notification being sent. Check the KVO-compliance of the ILViewController class.
  </code></pre>
  
  <p>Why is this happening? I have appropriately sent the KVO notifications. </p>
  
  <p>The following code, which uses an ivar and is actually NOT KVO compliant turns out to NOT result in any error....</p>
  
  <pre><code>@interface ILViewController : NSViewController {
      ILWindowController *_windowController;
  }
  
  - (ILWindowController *)windowController;
  
  @end
  
  @implementation ILViewController
  
  - (NSWindowController *)windowController {
      return _windowController;
  }
  
  - (void)setView:(NSView *)view {
      [super setView:view];
      _windowController = view.window.windowController;
  }
  
  @end
  </code></pre>
  
  <p>This is confusing the heck out of me... Can someone see why the first implementation is not KVO compliant?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>Got it, if the view is added to window after it is set for the ViewController then the view's window is indeed changed without appropriate KVO notification. However, when I try to observe the view's window's window controller, I'm getting errors like this</p>
  
  <pre><code>KVO autonotifying only supports -set&lt;Key&gt;: methods that return void. Autonotifying 
  will not be done for invocations of -[NSView _setWindow:].
  Cannot remove an observer &lt;NSKeyValueObservance 0x102e17880&gt; for the key path
  "window.windowController.mainStatus" from &lt;NSView 0x102e13ec0&gt;, most likely because
  the value for the key "window" has changed without an appropriate KVO notification
   being sent. Check the KVO-compliance of the NSView class.
  </code></pre>
  
  <p>So if you can't observe a view's window, surely there's gotta be some way to be notified when the view's window changes. Does anyone know how?</p>
  
  <p>Also, these still doesn't explain why the second version of the code that uses an iVar without KVO notification actually work. </p>