UITextField未更新以更改ObservableObject(SwiftUI)

UITextField未更新以更改ObservableObject(SwiftUI),swiftui,combine,observableobject,Swiftui,Combine,Observableobject,我正在尝试创建一个UITextView,用于编辑值currentDisplayedAddress。其他视图也会更改该值,我希望UITextView在发生这种情况时更新其文本 视图初始化正确,我可以从AddressTextField编辑currentDisplayedAddress,没有问题,并触发相关视图更新。但是,当值被其他视图更改时,文本字段的文本不会更改,即使textField.text在updateUIView中打印正确的更新值,其他视图也会相应更新 我不知道这可能是什么原因。非常感谢您

我正在尝试创建一个UITextView,用于编辑值
currentDisplayedAddress
。其他视图也会更改该值,我希望UITextView在发生这种情况时更新其文本

视图初始化正确,我可以从
AddressTextField
编辑
currentDisplayedAddress
,没有问题,并触发相关视图更新。但是,当值被其他视图更改时,文本字段的文本不会更改,即使
textField.text
updateUIView
中打印正确的更新值,其他视图也会相应更新

我不知道这可能是什么原因。非常感谢您的帮助

struct AddressTextField: UIViewRepresentable {
    private let textField = UITextField(frame: .zero)
    var commit: () -> Void
    @EnvironmentObject var userData: UserDataModel

    func makeUIView(context: UIViewRepresentableContext<AddressTextField>) -> UITextField {
        textField.text = self.userData.currentDisplayedAddress
        textField.delegate = context.coordinator
        return textField
    }

    func updateUIView(_ uiView: UITextField, context: UIViewRepresentableContext<AddressTextField>) {
        if self.textField.text != self.userData.currentDisplayedAddress {
        DispatchQueue.main.async {
            self.textField.text = self.userData.currentDisplayedAddress
            }
        }
    }

   (...)

    func makeCoordinator() -> Coordinator { Coordinator(self) }

    class Coordinator: NSObject, UITextFieldDelegate {
        var addressTextField: AddressTextField

        func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
            textField.resignFirstResponder()
            addressTextField.userData.currentDisplayedAddress = textField.text ?? String()
            addressTextField.commit()
            return true
        }
    }
}
struct AddressTextField:UIViewRepresentable{
私有let textField=UITextField(帧:.0)
var commit:()->Void
@EnvironmentObject var userData:UserDataModel
func makeUIView(上下文:UIViewRepresentableContext)->UITextField{
textField.text=self.userData.currentDisplayedAddress
textField.delegate=context.coordinator
返回文本字段
}
func updateUIView(uiView:UITextField,context:UIViewRepresentableContext){
如果self.textField.text!=self.userData.currentDisplayedAddress{
DispatchQueue.main.async{
self.textField.text=self.userData.currentDisplayedAddress
}
}
}
(...)
func makeCoordinator()->Coordinator{Coordinator(self)}
类协调器:NSObject,UITextFieldDelegate{
var addressTextField:addressTextField
func textField应该返回(textField:UITextField)->Bool{//delegate方法
textField.resignFirstResponder()辞职
addressTextField.userData.currentDisplayedAddress=textField.text??字符串()
addressTextField.commit()
返回真值
}
}
}

您不应该将
UITextField
创建为属性,因为在父级更新期间可以重新创建
AddressTextField
结构。
makeUIView
正是创建UI实例的地方,representable代表您处理其引用

这里是固定变量。使用Xcode 11.4/iOS 13.4进行测试

struct AddressTextField: UIViewRepresentable {
    var commit: () -> Void = {}
    @EnvironmentObject var userData: UserDataModel

    func makeUIView(context: UIViewRepresentableContext<AddressTextField>) -> UITextField {
        let textField = UITextField(frame: .zero)
        textField.text = self.userData.currentDisplayedAddress
        textField.delegate = context.coordinator
        return textField
    }

    func updateUIView(_ textField: UITextField, context: UIViewRepresentableContext<AddressTextField>) {
        if textField.text != self.userData.currentDisplayedAddress {
            textField.text = self.userData.currentDisplayedAddress
        }
    }

    func makeCoordinator() -> Coordinator { Coordinator(self) }

    class Coordinator: NSObject, UITextFieldDelegate {
        var addressTextField: AddressTextField
        init(_ addressTextField: AddressTextField) {
            self.addressTextField = addressTextField
        }
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
            textField.resignFirstResponder()
            addressTextField.userData.currentDisplayedAddress = textField.text ?? String()
            addressTextField.commit()
            return true
        }
    }
}
struct AddressTextField:UIViewRepresentable{
var commit:()->Void={}
@EnvironmentObject var userData:UserDataModel
func makeUIView(上下文:UIViewRepresentableContext)->UITextField{
设textField=UITextField(帧:.0)
textField.text=self.userData.currentDisplayedAddress
textField.delegate=context.coordinator
返回文本字段
}
func updateUIView(textField:UITextField,context:UIViewRepresentableContext){
如果textField.text!=self.userData.currentDisplayedAddress{
textField.text=self.userData.currentDisplayedAddress
}
}
func makeCoordinator()->Coordinator{Coordinator(self)}
类协调器:NSObject,UITextFieldDelegate{
var addressTextField:addressTextField
init(uAddressTextField:addressTextField){
self.addressTextField=addressTextField
}
func textField应该返回(textField:UITextField)->Bool{//delegate方法
textField.resignFirstResponder()辞职
addressTextField.userData.currentDisplayedAddress=textField.text??字符串()
addressTextField.commit()
返回真值
}
}
}

您不应该将
UITextField
创建为属性,因为在父级更新期间可以重新创建
AddressTextField
结构。
makeUIView
正是创建UI实例的地方,representable代表您处理其引用

这里是固定变量。使用Xcode 11.4/iOS 13.4进行测试

struct AddressTextField: UIViewRepresentable {
    var commit: () -> Void = {}
    @EnvironmentObject var userData: UserDataModel

    func makeUIView(context: UIViewRepresentableContext<AddressTextField>) -> UITextField {
        let textField = UITextField(frame: .zero)
        textField.text = self.userData.currentDisplayedAddress
        textField.delegate = context.coordinator
        return textField
    }

    func updateUIView(_ textField: UITextField, context: UIViewRepresentableContext<AddressTextField>) {
        if textField.text != self.userData.currentDisplayedAddress {
            textField.text = self.userData.currentDisplayedAddress
        }
    }

    func makeCoordinator() -> Coordinator { Coordinator(self) }

    class Coordinator: NSObject, UITextFieldDelegate {
        var addressTextField: AddressTextField
        init(_ addressTextField: AddressTextField) {
            self.addressTextField = addressTextField
        }
        func textFieldShouldReturn(_ textField: UITextField) -> Bool {   //delegate method
            textField.resignFirstResponder()
            addressTextField.userData.currentDisplayedAddress = textField.text ?? String()
            addressTextField.commit()
            return true
        }
    }
}
struct AddressTextField:UIViewRepresentable{
var commit:()->Void={}
@EnvironmentObject var userData:UserDataModel
func makeUIView(上下文:UIViewRepresentableContext)->UITextField{
设textField=UITextField(帧:.0)
textField.text=self.userData.currentDisplayedAddress
textField.delegate=context.coordinator
返回文本字段
}
func updateUIView(textField:UITextField,context:UIViewRepresentableContext){
如果textField.text!=self.userData.currentDisplayedAddress{
textField.text=self.userData.currentDisplayedAddress
}
}
func makeCoordinator()->Coordinator{Coordinator(self)}
类协调器:NSObject,UITextFieldDelegate{
var addressTextField:addressTextField
init(uAddressTextField:addressTextField){
self.addressTextField=addressTextField
}
func textField应该返回(textField:UITextField)->Bool{//delegate方法
textField.resignFirstResponder()辞职
addressTextField.userData.currentDisplayedAddress=textField.text??字符串()
addressTextField.commit()
返回真值
}
}
}