I have a program that uses 3 different windows, each in their own window group. In a button I can use openWindow() to call another window and it works perfectly. I watched the video at the link below and now am trying to convert my program to use a navigation stack instead of buttons. I got things to work using the code listed at the link with a few changes but am unable to get the navigation destination to use openWindow. I attempted to create a view builder but could not get it to work. After some additional research I am starting to think that the navigation destination expects to use the same window as the root view not a separate window/view and therefore, navigation destination wants just a view. But I am unsure. If someone could provide some guidance it would be appreciated.
https://developer.apple.com/videos/play/wwdc2022/10054/
struct ContentView: View { @Environment (\.openWindow) private var openWindow @StateObject private var dataModel = DataModel() var body: some View { VStack(alignment: .center) { NavigationStack() { List(Sections.allCases) { section in Section(section.localizedName) { ForEach(dataModel.stocks(in: section)) { stock in NavigationLink(stock.listingName, value: stock) .font(Font.custom("Arial", size: 16)) } } // end section } // end list .scrollContentBackground(.hidden) .background(Color.mint) .navigationTitle("Stocks") .navigationDestination(for: StockData.self) { stock in Text("\(stock.listingName) - \(stock.name)") // GenericTableView(content: openWindow(id: "table", value: stock.name)) // Cannot convert value of type '()' to expected argument type '() -> ContentView' } } // end navigation stack } // end v stack } } struct GenericTableView<Content:View>: View { let content: Content init(@ViewBuilder content: () -> Content) { self.content = content() } var body: some View { content } }
After doing more research it appears that this is not possible. So I converted the navigation stack to a series of buttons. It works. Below is an example button and my App struct.
Button { openWindow(id: "summary") } label: { Text("Summary") .font(Font.custom("Arial", size: 14.0)) .foregroundColor(Color.blue) .background(Color.clear) .padding(.leading, 35) } .focusable(false) .buttonStyle(.link) @main struct WindowGroupsApp: App { var body: some Scene { WindowGroup ("Home") { ContentView() .hostingWindowPosition(window: "Home") } Window("Summary", id: "summary") { SummaryView() .hostingWindowPosition(window: "Summary") } WindowGroup ("Table", id: "table", for: String.self) { $fundName in NavigationDestinationView(fundName: fundName!, numYears: 1) .hostingWindowPosition(window: "Table") } WindowGroup ("Chart", id: "chart1", for: String.self) { $fundName in CustomChartView(fundName: fundName!, numYears: 1) .hostingWindowPosition(window: "Chart") } WindowGroup ("Chart", id: "chart5", for: String.self) { $fundName in CustomChartView(fundName: fundName!, numYears: 5) .hostingWindowPosition(window: "Chart") } } }