Happy New Year everyone,
I am trying to convert one of my apps from NavigationStack
to NavigationSplitView
. I am having some trouble and I am sure I'm missing something, just not sure what. The below code is what I am using. The Progam
is a core data entity, lets say for testing purposes, its like this:
struct Program: : Identifiable, Hashable {
var id = UUID()
var name: String
var date: String
}
When I use the following show the split view, I am able to select the first program and the detail will show up in the detail section, however, when I select a different program the detail isn't changing. As you can see I put some print statements in and can see those get printed out when I select a different program, yet the detail isn't changing.
I am not sure what I'm missing, any help would be appreciated.
struct ListOfProgramsView: View {
// MARK: - Environment
@EnvironmentObject var dataManager: DataManager
// MARK: - State
@State private var selectedProgram: Program.ID?
@State private var currentSelectedProgram: Program?
var body: some View {
NavigationSplitView {
List(dataManager.programs, selection: $selectedProgram) { program in
ProgramsRowView(program: program)
.tag(program)
}
.navigationTitle(.nbProgramsViewTitle)
.onChange(of: selectedProgram) { newProgramSelection in
if let selection = newProgramSelection, let program = dataManager.programs.first(where: { $0.id == selection }) {
currentSelectedProgram = program
print("Changed Again: \(program.date.dateText(style: .medium))")
}
}
} detail: {
if let program = currentSelectedProgram {
ModifyExistingProgramView(program: program, dataManager: dataManager)
let _ = print("Program Date: \(program.date.dateText(style: .medium))")
} else {
NoProgramsView()
}
}
.navigationSplitViewStyle(.balanced)
}
}
Thanks,
Taz