0

我有一个文本字段,您可以在其中输入姓名和一个按钮以从联系人中进行选择,但是当我从联系人中选择时,文本字段不会更新并且看起来是空的(并保存空结果),但是当我再次按下按钮时,它会更新文本字段,一旦关闭工作表,我如何更新 texfield

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

    }
}
4

2 回答 2

0

我知道了!

.sheet(isPresented: self.$contactObj.showContactPicker, onDismiss:{self.opponentName = "\(self.contactObj.cObj.givenName) \(self.contactObj.cObj.familyName)"} ) {EmbeddedContactPicker(contactObj: self.contactObj)}
于 2020-04-27T08:45:34.220 回答
0

如果我正确理解了您的代码快照,contactObj那么ObservableObject解决方案可以如下

 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)}
于 2020-04-25T17:45:18.143 回答