gpt4 book ai didi

swift - 在不使用 Storyboard 的情况下从 NSWindowController 呈现模态

转载 作者:行者123 更新时间:2023-11-28 07:29:06 24 4
gpt4 key购买 nike

我构建了一个在登录过程开始时运行的 macOS 框架,我想在一个窗口中向用户显示一些信息。我有一个带有 .XIB 文件的 NSWindowController,它是我的框架的主窗口。

我想将更多 presentAsSheet 添加到另一个 View 。为此,我创建了一个带有 .XIB 的 NSViewController 来定义我想在模态中呈现的用户界面,但在这里我完全被阻止了,因为获取窗口的 contentViewController 总是 nil

我正在像这样初始化我的主窗口:

func run() {
NSApp.activate(ignoringOtherApps: true)
mainWC = MainWC(windowNibName: "MainWC")
guard mainWC.window != nil else {
return
}
NSApp.runModal(for: mainWC.window!)
}

我试图在其中展示另一个 NSViewController"

self.window?.contentViewController?.presentAsSheet(customViewController)

在我的 MainWindowController 中,我这样做:

@IBAction func btnAction(_ sender: Any) {
let customModal = CustomModal(windowNibName: "CustomModal")
self.window?.beginSheet(customModal.window!, completionHandler: { code in
print(message: "Clicked")
})
}

模态框从不出现

最佳答案

在加载窗口和内容 View 之前,您无法呈现 View

要么在 windowDidLoad() 的窗口 Controller 内

override func windowDidLoad() {
super.windowDidLoad()
// present the view
}

甚至在 View Controller 的 viewDidLoad

override func viewDidLoad() {
super.viewDidLoad()
// present the view
}

关于swift - 在不使用 Storyboard 的情况下从 NSWindowController 呈现模态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55560970/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com