0

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

    }
}
Roman R
  • 39
  • 8

2 Answers2

0

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)}
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • Thanks for your help, but it still works the same way... it is as if not self.opponentName should be used, but something $opponentName, for it to update the State, that would update the View – Roman R Apr 25 '20 at 18:00
  • @RomanR, would you show more code, so it was not needed to guess? – Asperi Apr 25 '20 at 18:11
  • here it is. Not that much of it – Roman R Apr 25 '20 at 18:19
0

I got it!

.sheet(isPresented: self.$contactObj.showContactPicker, onDismiss:{self.opponentName = "\(self.contactObj.cObj.givenName) \(self.contactObj.cObj.familyName)"} ) {EmbeddedContactPicker(contactObj: self.contactObj)}
Roman R
  • 39
  • 8