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      

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.