在 cocoa 应用中关闭点击时显示和隐藏 window 而不是终止应用
Show and hide window instead of terminating app on close click in cocoa app
我一直在尝试在关闭(红色按钮)时显示隐藏 window 单击 window。我想做的是隐藏 window ,当用户再次点击我的应用程序时,它会再次显示。
提前感谢所有提供答案的开发人员。我是 Cocoa 应用程序的新手。我是 iOS 开发人员,所以我对 cocoa 应用了解不多。
我也尝试隐藏(:) 方法和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
}
}
我想创建一个在后台运行的计时器应用程序,如果用户单击关闭,它将隐藏而不是终止。当他再次从停靠菜单中单击时,它将重新打开 window.
我不太喜欢 Mac OS 开发,但我认为你应该像这样从 NSWindowController
继承:
class MyWindowController: NSWindowController, NSWindowDelegate {
func windowShouldClose(_ sender: NSWindow) -> Bool {
NSApp.hide(nil)
return false
}
}
然后您只需打开您的 Main(或任何您拥有的名称)故事板,选择 Window Controller
并将您的 MyWindowController
设置为它:
我试过了,它对我有用。
我找到了解决办法。感谢@Silvester 建议提出 NSViewController。
按钮点击事件:
@IBAction func onButtonClick(_ sender: VSButton) {
let animator = ReplacePresentationAnimator()
let vc = self.storyboard?.instantiateController(withIdentifier: "identifier") as! yourVC
present(vc, animator: animator)
}
自定义动画师 class :
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 完美配合。谢谢。
我一直在尝试在关闭(红色按钮)时显示隐藏 window 单击 window。我想做的是隐藏 window ,当用户再次点击我的应用程序时,它会再次显示。
提前感谢所有提供答案的开发人员。我是 Cocoa 应用程序的新手。我是 iOS 开发人员,所以我对 cocoa 应用了解不多。
我也尝试隐藏(:) 方法和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
}
}
我想创建一个在后台运行的计时器应用程序,如果用户单击关闭,它将隐藏而不是终止。当他再次从停靠菜单中单击时,它将重新打开 window.
我不太喜欢 Mac OS 开发,但我认为你应该像这样从 NSWindowController
继承:
class MyWindowController: NSWindowController, NSWindowDelegate {
func windowShouldClose(_ sender: NSWindow) -> Bool {
NSApp.hide(nil)
return false
}
}
然后您只需打开您的 Main(或任何您拥有的名称)故事板,选择 Window Controller
并将您的 MyWindowController
设置为它:
我试过了,它对我有用。
我找到了解决办法。感谢@Silvester 建议提出 NSViewController。 按钮点击事件:
@IBAction func onButtonClick(_ sender: VSButton) {
let animator = ReplacePresentationAnimator()
let vc = self.storyboard?.instantiateController(withIdentifier: "identifier") as! yourVC
present(vc, animator: animator)
}
自定义动画师 class :
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 完美配合。谢谢。