OS X - How can a NSViewController find its window?

I have a Document based core data app. The main document window has a number of views, each controlled by its own custom NSViewController which are switched in as necessary. I want each of these view controllers to be able to drop down a custom modal sheet from the document window. However because the views are separate and not in the MyDocument nib I cannot link the view to the document window in IB. This means that when I call

[NSApp beginSheet: sheetWindow modalForWindow: mainWindow modalDelegate: self didEndSelector: @selector(didEndSheet:returnCode:contextInfo:) contextInfo: nil];

I’m supplying nil for mainWindow and the sheet therefore appears detached.

Any suggestions?

Many Thanks


Solution 1:

You can use [[self view] window]

Solution 2:

Indeed, it's self.view.window (Swift).

This may be nil in viewDidLoad() and viewWillAppear(), but is set properly by the time you get to viewDidAppear().