Nate Chandler
Nate Chandler

Reputation: 4553

Can a window be presented (as its own window, not as a sheet) modally for just one other window?

In Cocoa, is it possible to present a window modally as a window (not as a sheet) only with respect specifically to one other window? In particular, I would like to present an alert that prevents the user from interacting only with the window about which it is informing the user (until of course the user has dealt with--closed--the alert). While the user is prevented from interacting with the window specified in the call to present the alert, she is able to continue interacting with other windows. I'll reiterate: in this case presenting the alert as a sheet is not an admissible solution.

Upvotes: 0

Views: 125

Answers (1)

rickster
rickster

Reputation: 126127

Sheets are the standard method for making one window modal with respect to another -- especially for alerts -- so Apple does not provide convenient API for doing this some other way. Ask yourself:

  • Why do you consider a sheet an inadmissible solution?
  • What are you trying to do with this modal window that you can't do with a sheet?
  • What value is provided to your users by using uncommon UI for a common scenario?

If you really want to make a window modal to another without using the sheet API, implementing it is just a matter of considering what "window modal" means... which it seems you've already done:

... an alert that prevents the user from interacting only with the window ...

So what you really need to do is prevent the user from interacting with that window. There are many ways to do this, including:

  • Disable all controls in the window
  • Have any event-handling custom views in the window pay attention to whether your alert is visible, and ignore events in that case
  • Layer a view above all the window's content which prevents clicks from reaching views beneath
  • Subclass NSApplication and override sendEvent: to ignore events in the window (this is really overkill and potentially problematic)

In addition to that, you probably want to attach your alert to the window so they move together and don't do weird things in Exposé: see -[NSWindow addChildWindow:ordered:].

Upvotes: 1

Related Questions