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
NSViewController-Close window-Swift-Storyboard
提问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
- Be aware of each NSViewController & NSButton instance. Fix management of NSViewController & NSButton instances if needed.
- 注意每个 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
}
}
Set
isReleasedWhenClosed
in either the Interface Builder or in code, if needed.Closed the window via the NSButton
@IBAction
(e.g. Envoi) with one of the followingself.view.window!.windowController!.close()
NSWindowController close()abrupt close without possibility to ask user for confirmation.self.view.window!.close()
NSWindow close()sendswillCloseNotification
notification.self.view.window!.performClose(nil)
orself.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 objectwindowShouldClose(_:)
implementation. IfwindowShouldClose(_:)
returns true or is not implemented, thenNSWindow close()
is called. (performClose(_:)
is the more comprehensive and flexible choice)
设置
isReleasedWhenClosed
无论是在界面生成器或代码,如果需要的话。@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 中复制?这样做只会关闭您的新窗口,而不是第一个窗口。