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()
}
crashes like so
when closing window2 with the red dot in the top left corner.
Questions:
- Is there anything obviously wrong with my sample app
- Is SwiftUI best avoided for production grade apps targeting MacOS & iPadOS yet?
Thanks. Sonoma 14.4.1 Xcode 15.3