I have a program that has two main classes, Team and Player. Team has an array built in which holds 23 instances of the Player class, all with their own properties and methods. In the main content view of my app, I display a list of these homeTeam.players and each row has a .contextMenu with the option to call editName() from the individual Player.
When editName() is called it will open an alert and allow the user to enter a new name into the textField which then saves it and changes the name property of the Player.
My issue is that the list then does not update on the main view and the player name stays the same, however when you go to change it again, the name is in place in the textField, so it has changed, just isn't being displayed.
Player Class:
class Player{
var playerName: String = ""
var id = UUID()
init(playerName: String){
self.playerName = playerName
}
func editName{
//Displays alert with text field
self.playerName = newName
}
}
Team Class:
class Team{
var teamName: String = ""
var players: [Player] = [Player("Player 1"),
Player("Player 2"),
Player("Player 3"),
Player("Player 4")] //Continues for 23 Players
init(teamName: String){
self.teamName = teamName
}
}
Swift View:
List(homeTeam.players) {player in
HStack{
Text("\(player.shirtNumber) - \(player.playerName)")
Spacer()
Text("\(player.timerText)")
}
.contextMenu{
Button(action: {player.editName()}) {
Text("Edit Name")
Image(systemName: "square.and.pencil")
}
}
}
I'm still relatively new to SwiftUI, but have some experience with UIKit and storyboarding, so any help would be appreciated.