xcode NSViewController-关闭窗口-Swift-Storyboard

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33594710/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 08:10:44  来源:igfitidea点击:

NSViewController-Close window-Swift-Storyboard

xcodeswiftcocoa

提问by Fredo

I cannot find the right way to close the main window after I clicked the "proceed" button installed on this window. I have try to connect this button to the "proceedclose" option of the inspector. I have also try to insert the following line inside my code :

单击此窗口上安装的“继续”按钮后,我找不到关闭主窗口的正确方法。我尝试将此按钮连接到检查器的“proceedclose”选项。我还尝试在我的代码中插入以下行:

import Cocoa
class ViewController: NSViewController {
    ...
    @IBAction func Envoi(sender: NSButton) {    
        self.view.window!.close()
    }
}

None of them works, nothing happens and no error is reported. Could anyone help me to sort it out?

它们都不起作用,没有任何反应,也没有报告错误。谁能帮我整理一下?

回答by Set Minami

I had same question. And your code was very helpful for me.

我有同样的问题。你的代码对我很有帮助。

Then I checked "VC <- view <- window <- windowController" out in NS Lib. hierarchey has.

然后我在 NS Lib 中检查了“VC <- view <- window <- windowController”。层次结构有。

When you close window from VC, avoiding storyboard grow bizzare. you can solve this, by

当你从 VC 关闭窗口时,避免故事板变得奇怪。你可以解决这个问题,通过

self.view.window?.windowController?.close()

Just a quick note.

只是一个快速的笔记。

回答by l --marc l

  1. Be aware of each NSViewController & NSButton instance. Fix management of NSViewController & NSButton instances if needed.
  1. 注意每个 NSViewController 和 NSButton 实例。如果需要,修复 NSViewController 和 NSButton 实例的管理。
class ViewController: NSViewController {
    @IBOutlet weak var envoiButton: NSButton!
    …
    @IBAction func doEnvoiButtonPressed(_ sender: NSButton) {    
        print(self.hash)
        print(self.envoiButton.hash)
        // … other code
    }
}
  1. Set isReleasedWhenClosedin either the Interface Builder or in code, if needed.

  2. Closed the window via the NSButton @IBAction(e.g. Envoi) with one of the following

    • self.view.window!.windowController!.close()NSWindowController close()abrupt close without possibility to ask user for confirmation.
    • self.view.window!.close()NSWindow close()sends willCloseNotificationnotification.
    • self.view.window!.performClose(nil)or self.view.window!.performClose(self)NSWindow performClose(_:)sends to all window closing signals. First, windowShouldClose(_:)to window delegate if present. If no delegate is present, then invokes any NSWindow object windowShouldClose(_:)implementation. If windowShouldClose(_:)returns true or is not implemented, then NSWindow close()is called. (performClose(_:)is the more comprehensive and flexible choice)
  1. 设置isReleasedWhenClosed无论是在界面生成器或代码,如果需要的话。

  2. @IBAction使用以下任一方法通过 NSButton (例如 Envoi)关闭窗口

    • self.view.window!.windowController!.close()NSWindowController close()突然关闭而无法要求用户确认。
    • self.view.window!.close()NSWindow close()发送willCloseNotification通知
    • self.view.window!.performClose(nil)NSWindow performClose(_:)发送到所有窗口关闭信号。首先,如果存在窗口委托。如果不存在委托,则调用任何 NSWindow 对象实现。如果返回 true 或未实现,则调用。是更全面、更灵活的选择)self.view.window!.performClose(self)windowShouldClose(_:)windowShouldClose(_:)windowShouldClose(_:)NSWindow close()performClose(_:)
class ViewController: NSViewController {
    @IBAction func doEnvoiButtonPressed(_ sender: NSButton) {    
        self.view.window!.performClose(nil) // or performClose(self)
    }
}

回答by SammyRNYCreal

It is working by using "self.view.window!.close()" inside the button action, but when I add a new ViewController, and link the button to it, the first window does not disappear anymore.

它通过在按钮操作中使用“self.view.window!.close()”来工作,但是当我添加一个新的 ViewController 并将按钮链接到它时,第一个窗口不再消失。

I'm assuming you may have forgotten to duplicate the @IBAction func Envoi(sender: NSButton)in the new ViewController? By doing this only your new window will close and not the first window.

我假设您可能忘记@IBAction func Envoi(sender: NSButton)在新的 ViewController 中复制?这样做只会关闭您的新窗口,而不是第一个窗口。