in a book club app I'm trying to write, I have a NavigationSplitView which displays a list of readers in a selectable List that gives the user access to each reader's comments on a given book. At the top of the list is a button "Book" that takes the user back to the book's main page:
struct ProjectView: View
{
@State private var readers: [Reader]
@State private var selectedReader: Reader?
init(readers: [Reader])
{
self.readers = readers
}
var body: some View
{
NavigationSplitView
{
List(selection: self.$selectedReader)
{
Button("Book")
{
self.selectedReader = nil
}
Divider()
ForEach (self.readers, id: \.self)
{
reader in Text(reader.name)
}
}
}
detail:
{
if let selectedReader = self.selectedReader
{
ReaderView(reader: selectedReader)
}
else
{
BookView()
}
}
#if os(macOS)
.navigationSplitViewColumnWidth(min: 180, ideal: 200)
#endif
.navigationTitle(self.project.name)
}
}
I'm trying to make it so that "Book" will select / highlight just as the reader names in the reader list do, you can see what I mean in this video:
https://youtube.com/shorts/__IA890FxG0?feature=share
Does anyone know how to accomplish this? Or is it even possible?
self.readers = readers, it should be_readers = State(initialValue: readers), or uselet readers: [Reader]. If you need to change readers then use a@Binding. Also, you should not useForEach (self.readers, id: \.self), make the struct ReaderIdentifiable. How do you declareselectedReader? Note,Above the list of readers is a home button "Book" ...no, the button is at the top of the list, not above it.