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

Execute navigation link and a action

Forums > SwiftUI

Hi all,

Is there anyone who knows how you could display the contents of an array inside a list and have the row content navigate to a new view when you click on it AND execute an action?

I already found a solution online where they used ".simultaneousGesture(TapGesture())" but besides the fact I don't like the solution, it also does not work when used inside a List.

        List {
            ForEach(gameState.tables, id: \.self) { table in
                NavigationLink(destination: TableView()) {
                    TableRow(table: table.number, difficulty: table.difficulty)
                }
            }
        }

The code I now have is shown above but I also use an "@EnvironmentObject var gameState: GameState" which is shared across all views. Inside this gameState I have a property named "chosenTable". So I'd like to set this property when you click on the row and also navigate to the 'TableView' view. I have to use this property in future views as well which is why I don't want to pass it as an argument.

Any idea if such a thing (performing an action and navigate) is possible at all with SwiftUI?

2      

Hi Nico

I've found a possible solution. I don't know if you like it: How about using the NavigationLink(destination:tag:selection:) variation in combination with a custom binding to track the current selection?

I tested it successfully the code below. I had to use a func to create the binding, so I got around the weird compiler errors. Checkout Pauls quick start article on custom bindings.

BR Philipp

BTW: Instead of doing the custom binding below, simply use the NavigationLinks selection: argument directly on your choosenTable variable of your environment object?

struct ContentView: View {

    @ObservedObject var gameState: GameState

    @State var selectedTable: Int?

    var body: some View {
        NavigationView {
            List {
                ForEach(gameState.tables, id: \.self) { table in
                    NavigationLink(destination: TableView(),
                                   tag: table.number,
                                   selection: self.customBinding()) {
                        TableRow(table: table.number, difficulty: table.difficulty)
                    }
                }
            }
        }
    }

    func customBinding() -> Binding<Int?> {
        let binding = Binding<Int?>(get: {
            self.selectedTable
        }, set: {
            print("Table \(String(describing: $0)) chosen")
            self.selectedTable = $0
        })
        return binding
    }
}

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.