Check if NSViewController is presented as Popover

Hi,

I'm trying to figure out how an

NSViewController
can check if it was presented as a popvoer, so it might by notification and delegate pattern inform observers and let delegate act upon disappearing of the popover by overriding
viewWillDisappear
method.


So far I've with this solution:

- (BOOL)isPresentedAsPopover {
    id popoverFrameClass = NSClassFromString(@"NSPopoverFrame");
    id ourSuperViewClass = [[[self view] superview] class];
    return [ourSuperViewClass isEqualTo:popoverFrameClass];
}


I've found that when a

NSViewController
is presented as popover programmatically via
presentViewController:asPopoverRelativeToRect:ofView:preferredEdge:behavior:
method its view gets embedded in a
NSPopoverFrame
class view.

Which seems to be part of a class cluster pattern, hence it's a private class. I don't like this kind of solution, knowing that in future this could change etc.

Is there a better way to do that, rather than setting boolean property value in the View Controller when presenting it programmatically as a popover?

Could you provide some additional details about why the view controller needs to notify others about whether it's in a popover and that the popover disappeared?


As an example, NSPopover itself has notifications for when it is closed -- so the presenter of the view controller could observe those notifications.

>inform observers and let delegate act upon disappearing of the popover

As taylork notes, notifications are your friend (NSNotificationCenter / object) - that way you can have a given VC 'broadcast' info for another to act on, regardless of where they are, relative to each other, in that app's hierarchy, which can otherwise get messy when using normal call methods, as an example.

You can in Popover NSViewController did:

(view.accessibilityParent() as? NSWindow)?.close()
Check if NSViewController is presented as Popover
 
 
Q