简体   繁体   English

SwiftUI 视图未更新以反映 UIViewController 中的数据更改

[英]SwiftUI View Not Updating to Reflect Data Changes in UIViewController

I've been experimenting with SwiftUI and UIKit, trying to understand how data is shared between the two frameworks, and I've created a simple example for a larger project I am working on.我一直在试验 SwiftUI 和 UIKit,试图了解这两个框架之间如何共享数据,并且我为我正在处理的一个更大的项目创建了一个简单的示例。 The example is a single SwiftUI view that contains a UIViewControllerRepresentatable wrapping a custom view controller.该示例是单个 SwiftUI 视图,其中包含一个 UIViewControllerRepresentatable 包装了一个自定义视图 controller。 I am trying to have the SwiftUI view display the value of one of the view controller's properties, but it does not refresh correctly when the value is changed.我试图让 SwiftUI 视图显示视图控制器属性之一的值,但是当值更改时它不会正确刷新。

struct ContentView: View {
    @State var viewController = MyViewControllerRepresentable()
    var body: some View {
        VStack {
            viewController
            Text("super special property: \(viewController.viewController.data)")
        }
    }
}
class MyViewController: UIViewController, ObservableObject {
    @Published var data = 3

    override func viewDidLoad() {
        let button = UIButton(type: .system)
        button.setTitle("Increase by 1", for: .normal)
        button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
        view = button
    }

    @objc func buttonPressed() {
        data += 1
    }
}


struct MyViewControllerRepresentable: UIViewControllerRepresentable {
    @ObservedObject var viewController = MyViewController()

    func makeUIViewController(context: Context) -> UIViewController {
        return self.viewController
    }

    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

When I run the app and press the button, I can see that the actual value of data is changing, and the publisher in MyViewController is firing, but the value displayed on screen is not refreshed to reflect this.当我运行应用程序并按下按钮时,我可以看到data的实际值正在发生变化,并且MyViewController中的发布者正在触发,但屏幕上显示的值并未刷新以反映这一点。

Please note, I am very new to iOS development, and this is probably an unconventional data model.请注意,我对 iOS 开发非常陌生,这可能是非常规数据 model。 However, I don't see why it shouldn't work correctly.但是,我不明白为什么它不能正常工作。 Suggestions for a better way to share data would be much appreciated, but I would primarily like to know if it is possible to get this working with its current data structure.非常感谢有关共享数据的更好方法的建议,但我主要想知道是否可以使用其当前的数据结构来实现这一点。

Thank you in advance.先感谢您。

You could create a @Binding .您可以创建一个@Binding This means that when the value is updated for data , the views are recreated to reflect the changes.这意味着当为data更新值时,将重新创建视图以反映更改。

Here is how it can be done:这是如何完成的:

struct ContentView: View {

    @State private var data = 3

    var body: some View {
        VStack {
            MyViewControllerRepresentable(data: $data)
            Text("super special property: \(data)")
        }
    }
}


class MyViewController: UIViewController {

    @Binding private var data: Int

    init(data: Binding<Int>) {
        self._data = data
        super.init(nibName: nil, bundle: nil)
    }
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }


    override func viewDidLoad() {
        let button = UIButton(type: .system)
        button.setTitle("Increase by 1", for: .normal)
        button.addTarget(self, action: #selector(buttonPressed), for: .touchUpInside)
        view = button
    }

    @objc func buttonPressed() {
        data += 1
    }
}


struct MyViewControllerRepresentable: UIViewControllerRepresentable {

    @Binding var data: Int
    private let viewController: MyViewController

    init(data: Binding<Int>) {
        self._data = data
        viewController = MyViewController(data: data)
    }


    func makeUIViewController(context: Context) -> UIViewController {
        viewController
    }
    func updateUIViewController(_ uiViewController: UIViewController, context: Context) {}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM