gpt4 book ai didi

swift - macOS 上的 EnvironmentObject 和 SceneDelegate

转载 作者:行者123 更新时间:2023-12-04 10:56:54 24 4
gpt4 key购买 nike

我想使用 SwfitUI 在 macOS 上制作一些应用程序。

继iOS之后,还有SceneDelegate (在 iOS 13 之后)。但是在 macOS 中,只有 AppDelegate .

好的。我想用 environmentObjectSceneDelegatescene方法

window.rootViewController = UIHostingController(rootView: contentView
.environmentObject(mainController)
)

使用 @EnvironmentObject我的 ContentView 中的属性包装器.

从字面上看,我无法实现 .environmentObject()因为在 macOS 中没有 SceneDelegate !

我怎么解决这个问题? :(

请帮忙

最佳答案

这是为 macOS SwiftUI 项目默认生成的 AppDelegate,以及如何为其中的内容 View 设置环境对象。

func applicationDidFinishLaunching(_ aNotification: Notification) {
// Create content view
let contentView = ContentView().environmentObject(AppSettings())

// Create the window and set the content view.
window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 800, height: 600),
styleMask: [.titled, .closable, .miniaturizable],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName("Main Window")
window.contentView = NSHostingView(rootView: contentView)
window.makeKeyAndOrderFront(nil)
window.autorecalculatesKeyViewLoop = true
}

关于swift - macOS 上的 EnvironmentObject 和 SceneDelegate,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59114617/

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