SwiftUI How to update State property when if was updated from sheet view

377 Views Asked by At

I Have a textfield where you can enter name and a button to choose from contacts, but as I choose from contacts the textfield doesn't update and looks empty (and saves empty result), but as I press the button once more it updates the textfield, how do I update the texfield as soon as sheet is dismissed

struct AddResultView: View {
@State var nameOfGame: String = ""
@State var opponentName: String = ""
@State var dateOfGame = Date()
@State var myScore: String = ""
@State var opponentScore: String = ""
@State var isOn: Bool = true

@ObservedObject var games = AllGames()

@Environment(\.presentationMode) var presentationMode

@ObservedObject var contactObj: ContactObject

var body: some View {
    NavigationView {
        Form {
            Section {
                TextField("Name of the game", text: $nameOfGame)
                DatePicker("Pick date", selection: $dateOfGame, displayedComponents: .date)
                HStack {
                    TextField("Enter opponent or choose from Contacts", text: $opponentName)
                    Button(action: {
                        self.contactObj.showContactPicker.toggle()
                    }) {
                        Image(systemName: "person.crop.circle.badge.plus").foregroundColor(.accentColor)}
                }
                .onReceive(self.contactObj.$cObj) { cObj in
                    self.opponentName = "\(self.contactObj.cObj.givenName) \(self.contactObj.cObj.familyName)"}
            }
            .sheet(isPresented: self.$contactObj.showContactPicker) {EmbeddedContactPicker(contactObj: self.contactObj)}

            Section {
                TextField("My Score", text: $myScore)
                    .keyboardType(.numberPad)
                TextField("Opponent's Score", text: $opponentScore)
                    .keyboardType(.numberPad)

            }.onTapGesture {
                self.endEditing()
            }
            Section {
                Toggle(isOn: $isOn) {
                    Text("Bigger score wins")
                }
            }

            Button(action: {
                self.saveNewResult()
                self.presentationMode.wrappedValue.dismiss()
            }){
                Text("Submit")
            }
            .disabled(nameOfGame.isEmpty || opponentName.isEmpty || myScore.isEmpty || opponentScore.isEmpty)
        }
        .navigationBarTitle("Add New Result", displayMode: .inline)
        .keyboardResponsive()

    }
}
2

There are 2 best solutions below

0
On

I got it!

.sheet(isPresented: self.$contactObj.showContactPicker, onDismiss:{self.opponentName = "\(self.contactObj.cObj.givenName) \(self.contactObj.cObj.familyName)"} ) {EmbeddedContactPicker(contactObj: self.contactObj)}
3
On

If I understood correctly your code snapshot the contactObj is a ObservableObject then the solution can be as follows

 HStack {
        TextField("Enter opponent or choose from Contacts", text: $opponentName)
        Button(action: {
            self.contactObj.showContactPicker.toggle()
        }) {
            Image(systemName: "person.crop.circle.badge.plus").foregroundColor(.accentColor)}
    }
    .onReceive(self.contactObj.$cObj) { cObj in       // << here !!
        // update name after it is changed in from picker
        self.opponentName = "\(self.contactObj.cObj.givenName) \(self.contactObj.cObj.familyName)"
    }
    Text("\(self.contactObj.cObj.givenName) \(self.contactObj.cObj.familyName)")
}
.sheet(isPresented: self.$contactObj.showContactPicker) {EmbeddedContactPicker(contactObj: self.contactObj)}