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

Sheet contents won't update depending on parameters

Forums > SwiftUI

I am calling a sheet when a button is pressed. The button is generated through a ForEach loop and I want to pass the current object into the new sheet. Depending on this object I want the sheet to display different variables.

ForEach(processor.matchEvents){event in
    Button(action: {showSheet.toggle()}) {
        ZStack{
            RoundedRectangle(cornerRadius: 15)
             .fill(event.team.teamColor)
             .frame(minWidth: 150, maxHeight: .infinity)
             .padding([.vertical], 10)
             .padding([.horizontal], 5)

            VStack(alignment: .center, spacing: 5){
                Group{
                    Text(event.team.teamName)
                    Text(event.time)
                    Text(event.event)
                }

            }
        }
    }
     .sheet(isPresented: $showSheet){
         EditEventView(team: event.team, event: event)
    }
}

I have stripped out most styling to help shorten the code. Below is the code I am trying to run in the sheet, but am not getting the new updated event

struct EditEventView: View {
    @ObservedObject var team: Team
    var event: MatchEvent

    @Environment(\.presentationMode) var presentationMode

    @State private var playerSelect = 0

    @ObservedObject var processor = EventsProcessor.shared

    var body: some View{

        NavigationView{
            Form{
                Section(header: Text("Event")){
                    Text(team.teamName)
                    Text(event.event)
                    Text(event.player)
                    Text(event.time)

                }
            }
        }
    }
}

Each of the events are stored inside another bit of code, linked below. This is so they are accessible around the entire program.

struct MatchEvent: Identifiable{
    var id = UUID()

    var team: Team

    var time: String
    var event: String
    var player: String
}

class EventsProcessor: ObservableObject {
    static let shared = EventsProcessor()

    @Published var matchEvents = [MatchEvent]()

    func addEvent(time: String, event: String, player: String, team: Team){
        matchEvents.append(MatchEvent(team: team, time: time, event: event, player: player))
    }

}

I have got the sheets to display and that bit is working, however it will constantly display the same data and not update whenever I go to a new event. Any help would be appreciated.

3      

Your question is about displaying the correct data, which means we need to see where that data is being created. Currently that is not shown.

Also, since your ForEach is showing a list, why not use a List or a LazyVGrid you get the onTapGesture functionality for free there, and this helps more naturally display the right element in the sheet.

Which also means your addEvent function would be better changed to:

func add(matchEvent: MatchEvent) {
  matchEvents.append(matchEvent)
}

Point being, we need more context in order to be able to help out.

3      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.