![](/img/trans.png)
[英]Replace NSViewController under Swift2 Storyboard MAC OSX
[英]NSViewController-Close window-Swift-Storyboard
单击此窗口上安装的“继续”按钮后,我找不到关闭主窗口的正确方法。 我尝试将此按钮连接到检查器的“proceedclose”选项。 我还尝试在我的代码中插入以下行:
import Cocoa
class ViewController: NSViewController {
...
@IBAction func Envoi(sender: NSButton) {
self.view.window!.close()
}
}
它们都不起作用,没有任何反应,也没有报告错误。 有人可以帮我整理一下吗?
我有同样的问题。 你的代码对我很有帮助。
然后我在 NS Lib 中检查了“VC <- view <- window <- windowController”。 层次结构有。
当您从 VC 关闭窗口时,避免故事板变得奇怪。 你可以解决这个问题,通过
self.view.window?.windowController?.close()
只是一个快速的笔记。
class ViewController: NSViewController {
@IBOutlet weak var envoiButton: NSButton!
…
@IBAction func doEnvoiButtonPressed(_ sender: NSButton) {
print(self.hash)
print(self.envoiButton.hash)
// … other code
}
}
如果需要,请在 Interface Builder 或代码中设置isReleasedWhenClosed
。
通过 NSButton @IBAction
(例如 Envoi)使用以下方法之一关闭窗口
self.view.window!.windowController!.close()
NSWindowController close()突然关闭而不可能要求用户确认。self.view.window!.close()
NSWindow close()发送willCloseNotification
通知。self.view.window!.performClose(nil)
或self.view.window!.performClose(self)
NSWindow performClose(_:)发送到所有窗口关闭信号。 首先, windowShouldClose(_:)
到窗口委托(如果存在)。 如果不存在委托,则调用任何 NSWindow 对象windowShouldClose(_:)
实现。 如果windowShouldClose(_:)
返回 true 或未实现,则调用NSWindow close()
。 ( performClose(_:)
是更全面、更灵活的选择)class ViewController: NSViewController {
@IBAction func doEnvoiButtonPressed(_ sender: NSButton) {
self.view.window!.performClose(nil) // or performClose(self)
}
}
它通过在按钮操作中使用“self.view.window!.close()”来工作,但是当我添加一个新的 ViewController 并将按钮链接到它时,第一个窗口不再消失。
我假设您可能忘记在新的 ViewController 中复制@IBAction func Envoi(sender: NSButton)
? 这样做只会关闭您的新窗口,而不是第一个窗口。
以下项目很好地解决了这个问题。 感谢大家的回答。
您需要在发件人前面添加 _ ,一切都会起作用
import Cocoa
class ViewController: NSViewController {
...
@IBAction func Envoi(_ sender: NSButton) {
self.view.window!.close()
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.