[英]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.我想在func viewDidLoad()
隐藏/关闭我的主应用程序窗口,并且仅在某些事件需要时才显示/取消隐藏主窗口。
I tried self.view.window?.close()
but this leaves a white window.我试过self.view.window?.close()
但这会留下一个白色的窗口。 I also tried NSApp.hide(nil)
but then I can't unhide with NSApp.unhide(nil)
.我也试过NSApp.hide(nil)
但是我不能用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来自NSWindow 文档
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: Hide
和Close
是两个不同的东西:
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.调用orderOut(_:)
会导致窗口从屏幕上移除,但不会导致它被释放。 See theclose()
method for information on when a window is released.有关何时释放窗口的信息,请参阅close()
方法。 CallingorderOut(_:)
on a child window causes the window to be removed from its parent window before being removed.在子窗口上调用orderOut(_:)
会导致该窗口在被移除之前从其父窗口中移除。
Hiding the application ( NSApp.hide(nil
) is another different thing: It隐藏应用程序( NSApp.hide(nil
) 是另一回事:它
Hides all the receiver's windows, and the next app in line is activated.隐藏所有接收器的窗口,并激活下一个应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.