1

Here is a piece of my code for a games list view. If you select a game, you go to the second list view with players of the game. And then to a detail view about the players and the game combined. Also there are other views that need to reference the game. So I made the 'selected' game a EnvironmentObject var (which is an array with data). That way it is always available.

Since there is no need to pass 'game' to UserList, I removed it there so it's UserList().

Before the NavigationLink gets executed, I want to assign the selected game to selectedGame. How can I do that?

    @EnvironmentObject var selectedGame : Game

      ...

            List(games) { game in
                NavigationLink(destination: UserList()) {
                    CellRow(game: game)
                }
            }
arakweker
  • 1,535
  • 4
  • 18
  • 40
  • 1
    To pass an object, you would use `UserList().environmentObject(self.selectedGame)` as the destination for the link. I assume this selection was made somewhere else? You didn't mention how `selectedGame` was populated in the games list. – smr Sep 13 '19 at 20:15
  • I populated selectedGame in SceneDelegate.swift. When the user selects a game in the List(games) view, I want selectedGame be replaced with the selected game in the List. That doesn't work now... I still see the data I put in the SceneDelegate (in the UserList). – arakweker Sep 13 '19 at 20:30
  • 2
    Take a look at [this question](https://stackoverflow.com/questions/56706188/how-does-one-enable-selections-in-swiftuis-list) on how to get the selection from a list. Once you have the selection, you can pass that to the UserList view via a property on the view or an Environment variable – smr Sep 14 '19 at 00:50

0 Answers0