The following code assumes, that you have
- a NSViewController subclass that manages the popover view
- the view controller implements
NSPopoverDelegate
As hamstergene pointed out, the popover's window won't be available until it is shown.
When the popoverWillShow:
delegate method gets called, your popover view's window should be an instance of _NSPopoverWindow
(and not nil
).
- (void)popoverWillShow:(NSNotification *)notification
{
NSWindow* popOverWindow = self.view.window;
NSRect popoverRectInScreenCoords = [popOverWindow convertRectToScreen:self.view.frame];
NSLog(@"Popover Rect in Screen Coords:%@", NSStringFromRect(popoverRectInScreenCoords));
}
Update
Bryan (the OP) posted a solution to his problem in the comments.
The issue wasn't NSPopover
itself. The actual reason for the nil
window for some of his subviews was NSTableView
, which releases non-visible NSTableCellViews
.
As a workaround he implemented viewDidMoveToWindow
in his custom NSTableCellView
subclass. When viewDidMoveToWindow:
gets called, the table cell view already has a window that can be used to perform coordinate conversion.