NEW: My new book Pro SwiftUI is out now – level up your SwiftUI skills today! >>

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

   

Hacking with Swift is sponsored by Judo

SPONSORED Let’s face it, SwiftUI previews are limited, slow, and painful. Judo takes a different approach to building visually—think Interface Builder for SwiftUI. Build your interface in a completely visual canvas, then drag and drop into your Xcode project and wire up button clicks to custom code. Download the Mac App and start your free trial today!

Try now

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

Reply to this topic…

You need to create an account or log in to reply.

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.