我想用SwfitUI在macOS上做一些应用。
在iOS之后是SceneDelegate (在iOS 13之后)。但在macOS中,只有AppDelegate。
好的。我想在SceneDelegate的scene方法上使用environmentObject
window.rootViewController = UIHostingController(rootView: contentView
.environmentObject(mainController)
)在我的ContentView中使用@EnvironmentObject属性包装器。
从字面上讲,我不能在macOS中实现.environmentObject(),因为没有SceneDelegate!
我该怎么解决这个问题呢?
请帮帮忙
发布于 2019-11-30 19:24:25
下面是为macOS SwiftUI项目生成的默认AppDelegate,以及如何为其中的内容视图设置环境对象。
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
}https://stackoverflow.com/questions/59114617
复制相似问题