[英]Show and hide window instead of terminating app on close click in cocoa app
我一直試圖在關閉(紅色按鈕)上單擊顯示隱藏窗口。 我想做的是隱藏窗口,當用戶再次單擊我的應用程序時,它將再次顯示。
在此先感謝所有提供答案的開發人員。 我是可可應用程序的新手。 我是iOS開發人員,所以我對可可應用程序了解不多。
我也嘗試了hide( :)方法和orderOut( :)方法。 但不起作用。
class ViewController : NSViewController, NSWindowDelegates {
override func viewDidAppear() {
self.view.window?.delegate = self
}
func windowShouldClose(_ sender: NSWindow) -> Bool {
//NSApplication.shared.terminate(self)
//NSApp.hide(self)
//self.view.window?.orderOut(sender)
return false
}
}
我想創建一個計時器應用程序,如果用戶單擊關閉,它將在后台運行,它將隱藏而不是終止。 當他再次從停靠菜單中單擊時,它將重新打開窗口。
我找到了解決方案。 感謝@Silvester建議呈現NSViewController。 按鍵事件:
@IBAction func onButtonClick(_ sender: VSButton) {
let animator = ReplacePresentationAnimator()
let vc = self.storyboard?.instantiateController(withIdentifier: "identifier") as! yourVC
present(vc, animator: animator)
}
自定義動畫師類:
class ReplacePresentationAnimator: NSObject, NSViewControllerPresentationAnimator {
func animatePresentation(of viewController: NSViewController, from fromViewController: NSViewController) {
if let window = fromViewController.view.window {
NSAnimationContext.runAnimationGroup({ (context) -> Void in
fromViewController.view.animator().alphaValue = 0
}, completionHandler: { () -> Void in
viewController.view.alphaValue = 0
window.contentViewController = viewController
viewController.view.animator().alphaValue = 1.0
})
}
}
func animateDismissal(of viewController: NSViewController, from fromViewController: NSViewController) {
if let window = viewController.view.window {
NSAnimationContext.runAnimationGroup({ (context) -> Void in
viewController.view.animator().alphaValue = 0
}, completionHandler: { () -> Void in
fromViewController.view.alphaValue = 0
window.contentViewController = fromViewController
fromViewController.view.animator().alphaValue = 1.0
})
}
}
}
這將與Silvester MyWindowController完美配合。 謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.