你如何在 Swift 3.x 中使用 Storyboards/Cocoa 引用视图的窗口 [英] How do you reference the view's window in Swift 3.x using Storyboards/Cocoa
问题描述
随着 Xcode 和 Swift 的所有变化,我无法弄清楚如何在使用 XIB 的项目中处理 stotyboard 驱动的项目中的视图窗口.macOS 编程新手,如果这是基本内容,请提前道歉:
with all the changes in Xcode and Swift, I can't figure out how to address the view's window in a stotyboard-driven project in the way I'm doing it in projects using XIBs. New to macOS programming so apologies in advance if this is basic stuff:
使用情节提要时,我如何更改视图的窗口状态,例如:
When using a storyboard, how do I change the view's window state using for instance:
window.titleVisibility = .hidden
window.setContentSize(size)
在一个 xib 驱动的项目中,我使用
In an xib-driven project, I'm using
@IBOutlet weak var window: NSWindow!
但这似乎与故事板的工作方式不同.我如何使用故事板做同样的事情?任何帮助表示赞赏!
but this doesn't seem to work the same way with storyboards. How do I do the same tihing using storyboards? Any help appreciated!
推荐答案
您可以通过访问其 window 属性来获取视图窗口的引用.请注意,它不能在 view did load 内完成,但您可以使用延迟初始化程序为您的视图控制器创建一个窗口属性:
You can get a reference of your view window accessing its window property. Note that it can not be done inside view did load but you can create a window property for your view controller using a lazy initializer:
lazy var window: NSWindow! = self.view.window
并且您可以在方法 viewWillAppear 中进行窗口自定义:
And you can do your window customization inside the method viewWillAppear:
import Cocoa
class ViewController: NSViewController {
lazy var window: NSWindow! = self.view.window
override func viewWillAppear() {
super.viewWillAppear()
window.titleVisibility = .hidden
}
}
这篇关于你如何在 Swift 3.x 中使用 Storyboards/Cocoa 引用视图的窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!