UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

NavigationSplitView

Forums > SwiftUI

Good morning everyone,

I am new when it comes to NavigationSplitView. I'm not sure when you want to put a .navigationDestination. Here is my scenario. I have a view that has a NavigationSplitView in it. Like this:

        NavigationSplitView(columnVisibility: $visibility) {
            VStack(alignment: .center) {
                // This shows a segmented control to user so they can choose between
                // completed and scheduled programs
                ChooseWhichProgramsToView(
                    whichProgramsToShowChoice: $showCompletedOrScheduledProgramsChoice,
                    currentSelectedProgram: $currentSelectedProgram,
                    sortProgramItemsByNameDateTypeChoice: $sortProgramItemsByDateChoice,
                    itemCount: countOfPrograms()
                )

                // Where we see the list of programs associated as either scheduled
                // or completed.
                ShowProgramsContainerView(
                    currentSelectedProgram: $currentSelectedProgram,
                    whichProgramsToShowChoice: $showCompletedOrScheduledProgramsChoice,
                    sortProgramItemsByNameDateTypeChoice: $sortProgramItemsByDateChoice,
                    searchTerm: $searchTerm
                )
            }
            .navigationTitle(showCompletedOrScheduledProgramsChoice == 0 ? .scheduledProgramsTitle : showCompletedOrScheduledProgramsChoice == 1 ? .completedProgramsTitle : .allProgramsTitle)
            .navigationBarTitleDisplayMode(.inline)
            .searchable(text: $searchTerm, placement: .navigationBarDrawer(displayMode: .always), prompt: Text(.searchByProgramName))
            .toolbar {
                returnTrailingNavBarItems()
            }
            .toastView(toast: $toast)
        } detail: {
            if let program = currentSelectedProgram {
                withAnimation {
                    DetailOfSelectedProgramView(program: program)
                }
            } else {
                withAnimation {
                    NoProgramsSelectedView()
                }
            }
        }
        .navigationSplitViewStyle(.balanced)
        .onDisappear {
            currentSelectedProgram = nil
        }

The ShowProgramsContainerView is this

        VStack {
            if whichProgramsToShowChoice == 0 {
                // Inside here we are working with scheduled programs
                if dm.getAllProgramsByStatus(byNameDateType: sortProgramItemsByNameDateTypeChoice,
                                            programStatus: Status.scheduled).isEmpty {
                } else if dm.getAllProgramsByStatus(byNameDateType: sortProgramItemsByNameDateTypeChoice,
                } else {
                    List(selection: withAnimation { $currentSelectedProgram } ) {
                        ForEach(dm.getAllProgramsByStatus(byNameDateType: sortProgramItemsByNameDateTypeChoice,
                             programStatus: Status.scheduled).filter {
                            searchTerm.appearsIn($0.name) || searchTerm.appearsIn($0.date.dateText(style: .medium))
                        }, id: \.self) { program in
                            NavigationLink(value: program) {
                                ListOfProgramsRowView(program: program)
                            }
                        }
                        .onDelete(perform: deleteAllScheduledPrograms)
                    }
                    .tint(Color.listRowHightlight)
                    .listStyle(.plain)
                }
            } else if whichProgramsToShowChoice == 1 {
              // I took the code out here its the same as above. Shows the list of programs
            } else {
              // I took the code out here its the same as above. Shows the list of programs
            }
        }
    }

As you can see I have a list with with a binding to the currentSelectedProgram. What I need to know is do I need to have NavigationLink(value: program) here. I struggle with when you need to use NavigationLink and .navigationDestination.

If someone could shed some light on this I'd appreciate it. If I remove NavigationLink the program still navigates to the view in the detail section.

I hope all this was clear and apologize for the length. I tried to shorten it as much as possible.

Thanks Taz

1      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.