首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >macOS上的EnvironmentObject和SceneDelegate

macOS上的EnvironmentObject和SceneDelegate
EN

Stack Overflow用户
提问于 2019-11-30 16:49:43
回答 1查看 782关注 0票数 2

我想用SwfitUI在macOS上做一些应用。

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

好的。我想在SceneDelegatescene方法上使用environmentObject

代码语言:javascript
复制
window.rootViewController = UIHostingController(rootView: contentView
            .environmentObject(mainController)
        )

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

从字面上讲,我不能在macOS中实现.environmentObject(),因为没有SceneDelegate

我该怎么解决这个问题呢?

请帮帮忙

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-11-30 19:24:25

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

代码语言:javascript
复制
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
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59114617

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档