I have taken the liberty of tweaking your code a bit which also fixes your issue of not being able to delete a page from sidebar list which shows all your pages in the wiki.
Wiki and PageView stay the same.
PageList
struct PageList: View {
@Binding var wiki: Wiki
@Binding var selection: Page?
var body: some View {
List(selection: $selection) {
Section {
ForEach($wiki.pages, id: \.id) { $page in
NavigationLink(value: page) {
TextField("", text: $page.title)
}
.contextMenu {
Button("Delete Page") {
wiki.pages.removeAll(where: { $0.id == page.id })
}
}
}
} header: {
Text("Pages")
}
}
.toolbar {
ToolbarItemGroup (placement: .navigation) {
Spacer()
Button {
wiki.pages.append(.init(title: "New Page", text: ""))
} label: {
Label("Add", systemImage: "plus")
}
}
}
}
}
List stores the selection. ForEach is added with stores the $wiki.pages array. NavigationLink is updated to use the new .navigationDestination() modifier in ContentView. Added a contextMenu() to the NavigationLink. Added an Add Button in the toolbar.
ContentView
struct ContentView: View {
@Binding var wiki: Wiki
@State private var selection: Page? = nil
var body: some View {
NavigationSplitView {
PageList(wiki: $wiki, selection: $selection)
.navigationSplitViewColumnWidth(ideal: 192)
.navigationDestination(item: $selection) { selection in
PageView(page: selection)
}
} detail: {
Text("Select a page to view its contents.")
}
.onAppear {
selection = wiki.pages.first
}
}
}
Detail View only shows the Text because the .navigationDestination() now handles the PageView. If you have persistence, then the onAppear will work but otherwise it doesn't do anything as of right now since the array will be wiped each time you relaunch the app.
I would also suggest changing @Binding var wiki: wiki to an @State variable if you do not want your entire app across all open windows to share the same state. For example: I open a new window to edit a different Wiki. However since the state is at the App level, every WindowGroup will show the exact same Wiki.
LMK if this resolves your issue.
Topic:
UI Frameworks
SubTopic:
SwiftUI