ios - SwiftUI View 未更新以反射(reflect) UIViewControlle

我一直在试验 SwiftUI 和 UIKit,试图了解数据是如何在这两个框架之间共享的,并且我已经为我正在处理的一个更大的项目创建了一个简单的示例。该示例是单个 SwiftUI View ,其中包含一个 UIViewControllerRepresentatable 包装自定义 View Controller 。我试图让 SwiftUI View 显示 View Controller 属性之一的值,但是当值更改时它没有正确刷新。

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

当我运行应用程序并按下按钮时,我可以看到 data 的实际值正​​在发生变化,并且 MyViewController 中的发布者正在触发,但是值屏幕上显示的内容不会刷新以反射(reflect)这一点。

请注意,我是 iOS 开发的新手,这可能是一个非常规的数据模型。但是,我不明白为什么它不能正常工作。非常感谢有关更好的数据共享方式的建议,但我主要想知道是否有可能让它与当前的数据结构一起工作。

提前谢谢你。

最佳答案

您可以创建一个@Binding。这意味着当 data 的值更新时,将重新创建 View 以反射(reflect)更改。

具体做法如下:

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

关于ios - SwiftUI View 未更新以反射(reflect) UIViewController 中的数据更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61369906/

相关文章:

ionic-framework - Ionic 5 键盘的问题

perl - 使用 Perl 连接到 AWS Managed Cassandra

flutter - 如何在 Flutter 中禁用 SnackBar 的淡入/淡出动画?

python - 当没有 activate.bat 并且我是凡人时,如何在虚拟环境中的任务调度程序中

php - 更改 Woocommerce 管理员订单列表时间和日期格式

javascript - postgres : relation does not exist wh

node.js - 如何增加 Node.js 中的最大调用堆栈大小

google-app-engine - 将 AppEngine 中的传入请求路由到不同版本

ios - 如何在 SwiftUI 中更新 TextField 的值?

reactjs - Ant Design 如何处理 Form.List 从底层数据结构访问值?