SwiftUI如何从图纸视图更新状态属性

时间:2020-04-25 17:34:20

标签: swiftui uiviewrepresentable contactsui

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

<script src="https://cdnjs.cloudflare.com/ajax/libs/gsap/3.2.4/gsap.min.js"></script>
<div class="content content__first">
  <div class="content__move">
    <div class="content__reverse">
      <div class="intro">
        <h1>This is blurry</h1>
      </div>
    </div>
  </div>
</div>

2 个答案:

答案 0 :(得分:0)

如果我正确理解了您的代码快照contactObjObservableObject,则解决方案可以如下

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

答案 1 :(得分:0)

我明白了!

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