Search code examples
windowdetachnspopover

NSPopover sample code does not detach window


I have not been able to get my NSPopover to detach to a window in my own projects, so to simplify I tried the Apple sample.

I downloaded a fresh copy of the Apple sample project: http://developer.apple.com/library/mac/samplecode/Popover/Introduction/Intro.html

It behaves the same, which is to say I can't drag the window to detach it either.

The project seems to provide all the correct windows and controllers and implements the detachableWindowForPopover: delegate method. However the method is never called.

Does anyone know the secret to detachable NSPopovers?


Solution

  • Found the answer while typing the question…

    Mac OS X 10.10 Yosemite has a new delegate method:

    • (BOOL)popoverShouldDetach:(NSPopover *)popover

    The default behavior on Yosemite is NO (should not detach). So delegates must implement this method in order for windows to be detachable. The sample project does not implement this method, so when compiled on Yosemite it will not detach (and also produces several deprecation warnings -- maybe I should have taken that hint that it needs an update).

    Adding:

    - (BOOL)popoverShouldDetach:(NSPopover *)popover {
        return YES;
    }
    

    To MyWindowController.m fixes the problem.