Reputation: 294
I wanna make some app on macOS using SwfitUI.
Following the iOS, there is SceneDelegate
(after iOS 13). But in macOS, there is only AppDelegate
.
OK. I wanna use environmentObject
on SceneDelegate
's scene
method
window.rootViewController = UIHostingController(rootView: contentView
.environmentObject(mainController)
)
to use @EnvironmentObject
property wrapper in my ContentView
.
Literally I can't implement .environmentObject()
cuz in macOS there is no SceneDelegate
!
How can I solve this problem?
Upvotes: 3
Views: 1424
Reputation: 257719
Here is default generated AppDelegate for macOS SwiftUI project, and how environment object can be set for content view in it.
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
}
Upvotes: 3