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

SOLVED: Need help, NavigationLink not working?

Forums > SwiftUI

Hi all,

I have a view that has a Navigation Link in it and the link is not working, when you tap on it it does nothing. I've even put a print statement in there and nothing happens.

Can anyone help me with this or point me in the right direction?

I would sure appreciate it. Here is the code I'm using:

    @Environment(\.presentationMode) var presentationMode
    @Environment(\.managedObjectContext) var moc
    @State private var showAddHymnsToPlaylist = false
    @State private var editMode: EditMode = .inactive
    var playList: Playlist

   var body: some View {
        VStack {
            if playList.theHymns.count == 0 {
                NoPlaylistHymnsView(showNoPlaylistHymnsView: $showAddHymnsToPlaylist, nameOfPlaylistHymns: "Hymns")
            } else {
                List {
                    if editMode == .active {
                        NavigationLink(destination: AddHymnsToPlaylistSheetView(playlistName: self.playList)) {
                            Text("Add Hymns ...").font(.headline).foregroundColor(Color.themeAccent)
                        }
                    }
                    ForEach(0..<playList.theHymns.count, id: \.self) { hymn in
                        PlaylistHymnDetailRowView(hymn: self.playList.theHymns[hymn])
                    }
                    .onDelete(perform: deleteHymns)
                }
            }
        }
        .navigationBarTitle("\(playList.nameOfPlaylist ?? "Unknown Name")", displayMode: .inline)
        .navigationBarItems(trailing:
            HStack(alignment: .center, spacing: 20) {
                EditButton()

                Button(action: {
                    // MARK: Code Here
                }) {
                    Image(systemName: "arrow.up.square").font(.title)
                }
            }
        )
        .environment(\.editMode, self.$editMode)
    }

2      

NavigationLink is not intended to work when in edit mode. Editing a list should not require navigating to another screen. Consider an add button in the navigation header or some other means of allowing the user to add a list item.

Also, NavigationLink isn't the proper way to display a sheet, which I assume you are trying to do based on the AddHymnsToPlaylistSheetView name. Again, a dedicated add button that shows the sheet is a better way to go here.

2      

@roosterBoy, thanks. That makes sense. I just thought since I was in a NavigationView that I could do that. Well now I know that editMode doesn't allow a link to work. I'll add a button in the Navbar. Thanks again for the help.

2      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.