简体   繁体   中英

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)
   }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM