struct ContentView: View {
var body: some View {
#if os(macOS)
VStack {
Button("Open Window 2") {
Window2().openInWindow(title: "Window 2", sender: self)
}
.contextMenu {
Button("window1menuItem") {
// Action for window1menuItem
}
}
Image(systemName: "globe")
.imageScale(.large)
.foregroundStyle(.tint)
Text("Hello, world!")
}
#endif
}
}
struct Window2: View {
var body: some View {
Text("This is Window 2")
.frame(width: 300, height: 200)
.contextMenu {
Button("window2menuItem") {
// Action for window2menuItem
}
}
}
func openInWindow(title: String, sender: Any?) {
#if os(macOS)
let window = NSWindow(
contentRect: NSRect(x: 20, y: 20, width: 280, height: 200),
styleMask: [.titled, .closable, .miniaturizable, .resizable],
backing: .buffered, defer: false)
window.center()
window.setFrameAutosaveName(title)
window.contentView = NSHostingView(rootView: self)
window.makeKeyAndOrderFront(sender)
#endif
}
}
#Preview {
ContentView()
}
when closing window2 with the red dot in the top left corner. closing window1 does not crash the app. Closing window2 after closing window1 does crash the app just the same as did closing window2 with window1 open.
Questions:
Separate openInWindow
use something like NSHostingView(rootView: Window2())
.
SwiftUI has openWindow
Environment
property too.
In your code self
is getting killed instantly.
https://www.hackingwithswift.com/quick-start/swiftui/how-to-open-a-new-window