I have three Views in my app. The first one shows a List of Games. The second has a list of players for each game. And the third one has a score-List for each player. So the structure looks like this:
struct GameView: View {
@environmentObject var model: Model
var body: some View {
NavigationView {
List(model.games) {game in
NavigationLink(destination: PlayerView(game: game)) {
// Some View
}
}
}
}
}
struct PlayerView: View {
@environmentObject var model: Model
var game Game
var gameIndex: Int {
model.games.firstIndex() {$0 == game}!
}
var body: some View {
TextField("Game", $model.games[gameIndex].title)
List(game.players) {player in
NavigationLink(destination: ScoreView(player: player, gameIndex: gameIndex)) {
// Some View
}
}
}
}
struct ScoreView: View {
@environmentObject var model: Model
var player: Player
var gameIndex: Int
var playerIndex: Int {
model.games[gameIndex].players.firstIndex() { $0 == player }!
}
var body: some View {
TextField("Player", $model.games[gameIndex].players[playerIndex].name)
List(player.scores) {score in
// Some View
}
}
}
My problem is: For every view that goes deeper into the hierarchy I have to go all the way back to my environmentObject and get the path through my model-arrays with indexes, that I have to pass through every view. I want every view being updated if I change the TextField value (just as an example for every data changing in the app) and if I'm moving back- and forwards through the NavigationView. I'm sure, there is some bad construction error, but I don't get the right answer.
$model.games[gameIndex].players[playerIndex].name, correct? I don't really understand the last sentence of the question. - Benjamin KindleListandNavigationViewinGameView. You only want oneNavigationView, but by putting it insideListaNavigationViewgets created for every item in the list. - Benjamin Kindle