显示和隐藏窗口,而不是在可可应用程序中单击关闭时终止应用程序 [英] Show and hide window instead of terminating app on close click in cocoa app

查看:104
本文介绍了显示和隐藏窗口,而不是在可可应用程序中单击关闭时终止应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图显示关闭时的隐藏窗口(红色按钮),然后单击该窗口.我想做的是隐藏窗口,当用户再次单击我的应用程序时,它将再次显示.

I have been trying to show hide window on close(red button) click on the window. I would like to do is hide the window and when a user clicks on my app again it will show again.

首先感谢所有提供答案的开发人员.我是可可应用程序的新手.我是iOS开发人员,所以我对可可应用程序了解不多.

Thanks in advance to all the developers who provide an answer. I am new to Cocoa apps. I am iOS developers so I have not much knowledge about cocoa apps.

我也尝试隐藏( :)方法和orderOut( :)方法.但不起作用.

I tried to hide(:) method and orderOut(:) method too. but not working.

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

我想创建一个计时器应用程序,如果用户单击关闭,它将在后台运行,它将隐藏而不是终止.然后当他再次从停靠菜单中单击时,它将重新打开窗口.

I want to create a timer app which runs in the background if user click on close it will hide instead of terminating. and when he clicks again from dock menu it will reopen the window.

推荐答案

我不太喜欢Mac OS的开发,但是我认为您应该像这样从NSWindowController继承:

I'm not much into Mac OS development, but I think you should inherit from NSWindowController like this:

class MyWindowController: NSWindowController, NSWindowDelegate {

    func windowShouldClose(_ sender: NSWindow) -> Bool {
        NSApp.hide(nil)
        return false
    }
}

然后,您只需要打开Main(或您使用的任何名称)情节提要,选择Window Controller并将其设置为MyWindowController:

Then you need just open your Main (or whatever name you have) storyboard, choose Window Controller and set your MyWindowController to it:

我尝试了,对我有用.

这篇关于显示和隐藏窗口,而不是在可可应用程序中单击关闭时终止应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆