実現したいこと
新しいタブを自由に作成&削除できるようしたい
発生している問題・分からないこと
新しいタブを作成してそのタブを消すとアプリが終了してしまう
エラーメッセージ
error
1Thread 1: EXC_BREAKPOINT (code=1, subcode=0x1924b23c4)
該当のソースコード
swift
1 // 新しいタブ作成関数 2 @objc func newTab() { 3 guard let currentWindow = NSApplication.shared.keyWindow else { return } 4 5 let newWindow = NSWindow( 6 contentRect: NSRect(x:0, y: 0, width: 800, height: 600), 7 styleMask: [.titled, .closable, .resizable, .miniaturizable], 8 backing: .buffered, 9 defer: false 10 ) 11 newWindow.title = "新しいタブ" 12 13 let contentView = TextEditorView().environmentObject(self) 14 let hostingView = NSHostingView(rootView: AnyView(contentView)) // AnyViewでラップ 15 newWindow.contentView = hostingView 16 17 currentWindow.addTabbedWindow(newWindow, ordered: .above) 18 newWindow.makeKeyAndOrderFront(nil) 19 newWindow.delegate = self 20 }
swift
1import SwiftUI 2 3@main 4struct MyApp: App { 5 @NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate 6 7 var body: some Scene { 8 WindowGroup { 9 TextEditorView() 10 .environmentObject(appDelegate) 11 .environment(\.colorScheme, appDelegate.isDarkMode ? .dark : .light) 12 .preferredColorScheme(appDelegate.isDarkMode ? .dark : .light) 13 .frame(minWidth: 700, minHeight: 600) 14 } 15 } 16}
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
コード色々変更しても解決できなかった
補足
特になし

あなたの回答
tips
プレビュー