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()
}
}