doom4
doom4

Reputation: 695

Programmatically hide and close the window of current NSViewController

I would like to hide/close my main app window in func viewDidLoad() and only show/unhide the main window if some event requires it.

I tried self.view.window?.close() but this leaves a white window. I also tried NSApp.hide(nil) but then I can't unhide with NSApp.unhide(nil). Here is some sample code:

override func viewDidLoad() {
   super.viewDidLoad()
   // Do any additional setup after loading the view.
   NSApp.hide(nil)
   runTest()
}

func runTest () {
   let check = false

   if check == false {
      NSApp.unhide(nil)
   }
}

Upvotes: 0

Views: 3379

Answers (1)

vadian
vadian

Reputation: 285079

From the NSWindow documentation

func orderOut(_ sender: Any?)

Removes the window from the screen list, which hides the window.

func makeKeyAndOrderFront(_ sender: Any?)

Moves the window to the front of the screen list, within its level, and makes it the key window; that is, it shows the window.

Hide and Close are two different things:

If the window is the key or main window, the window object immediately behind it is made key or main in its place. Calling orderOut(_:) causes the window to be removed from the screen, but does not cause it to be released. See the close() method for information on when a window is released. Calling orderOut(_:) on a child window causes the window to be removed from its parent window before being removed.

Hiding the application (NSApp.hide(nil) is another different thing: It

Hides all the receiver’s windows, and the next app in line is activated.

Upvotes: 2

Related Questions