swift - macOS 上的 EnvironmentObject 和 SceneDelegate
问题描述
我想使用 SwfitUI 在 macOS 上制作一些应用程序。
在 iOS 之后,还有SceneDelegate
(iOS 13 之后)。但在 macOS 中,只有AppDelegate
.
好的。我想用environmentObject
onSceneDelegate
的scene
方法
window.rootViewController = UIHostingController(rootView: contentView
.environmentObject(mainController)
)
在@EnvironmentObject
我的ContentView
.
从字面上看,我无法.environmentObject()
在 macOS 中实现因为没有SceneDelegate
!
我怎么解决这个问题?:(
请帮忙
解决方案
这是为 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
}
推荐阅读
- angularjs - 如何在 angularJS 中执行 routeGuard
- html - 滚动时如何使div同时向右和向左粘
- service - 您能告诉我们地图上的数据什么时候更新吗?
- ruby - Ruby Enumerators 链接是如何工作的?
- flutter - 如何在单页 Flutter Web 应用中添加 Google 站点链接搜索框
- ms-access - 如何设置交叉表查询来计算负库存计数的天数?
- node.js - 我如何收集 dms 并将它们发送到服务器上的频道
- node.js - 在 github 中上传 reactjs 代码,没有 node_modules 文件夹
- php - 使用PHP,我想在一定数量的单词之后分割一个字符串
- node.js - 更改任何用户信息后自动注销passport-local-mongoose