TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

SOLVED: Updating Lists in SwiftUI after property changes

Forums > SwiftUI

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.

3      

hi Ben,

a SwiftUI view will update in response to a change in a @State variable or an @ObservedObject -- one that conforms to the ObservableObject protocol. my guesses are that the following will help (we don't see the full view code, so a little guessing is required):

  • add conformance of Team to ObservableObject
  • be sure homeTeam is marked as @ObservedObject var homeTeam: Team
  • when it comes time to edit a player's name, call a (new) method on Team, not the Player, perhaps named changeName(player: Player, to newName: String) to make the change for you
  • be sure that changeName() calls the Team method objectWillChange.send() so the view knows that the team has changed (this method is part of the ObservedObject protocol). that's what causes the view to redraw.

again, that's my best guess, based on what you showed.

if at any time in the future you replace the explicit HStack { } in your List by a subview, perhaps written as PlayerView(player: player), you'll then want to also have Player conform to ObservableObject and be marked as @ObservedObject in that subview.

hope that helps,

DMG

5      

Hacking with Swift is sponsored by Blaze.

SPONSORED Still waiting on your CI build? Speed it up ~3x with Blaze - change one line, pay less, keep your existing GitHub workflows. First 25 HWS readers to use code HACKING at checkout get 50% off the first year. Try it now for free!

Reserve your 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.