gpt4 book ai didi

ios - 嵌入在 UIViewController 中的 UIHostingController - 如何从外部更新 @State?

转载 作者:行者123 更新时间:2023-12-05 00:53:53 36 4
gpt4 key购买 nike

我正在使用 UIHostingControllerContentView 嵌入到 ViewController 中。当按下“更改名称”按钮时,我想更改 ContentViewname 的名称。这是我的代码:

class ViewController: UIViewController {

var contentView: ContentView? /// keep reference to ContentView

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = UIColor.secondarySystemBackground

/// add the button
let button = UIButton()
button.frame = CGRect(x: 50, y: 50, width: 200, height: 100)
button.setTitle("Change name", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
view.addSubview(button)

/// add the SwiftUI ContentView
let contentView = ContentView()
let hostingController = UIHostingController(rootView: contentView)
self.contentView = contentView

addChild(hostingController)
view.insertSubview(hostingController.view, at: 0)
hostingController.view.frame = CGRect(x: 0, y: 400, width: view.bounds.width, height: view.bounds.height - 400)
hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
hostingController.didMove(toParent: self)
}

@objc func handleTap() {
contentView?.updateLastCardName(name: "Updated name") /// update the name
}
}

struct ContentView: View {
@State var name = "Name"

var body: some View {
Text(name)
}

func updateLastCardName(name: String) {
print("updating to \(name)")
self.name = name /// but it's not updating!
}
}

结果:

问题是,即使 func updateLastCardName(name: String) { 被调用,当我设置 self.name = name 时没有任何变化。 Text 继续显示“名称”,而不是“更新的名称”。

我读过 @State should be local ,所以我尝试使用 updateLastCardName 函数解决这个问题。但是,它不起作用。我的方法错了吗?

如何从 ViewController 更新 ContentViewname

最佳答案

var contentView: ContentView? /// keep reference to ContentView

这是错误的假设,因为 ContentView 是一个结构,即。值(value),因此您保留副本而不是引用。

相反,我们应该使用 View 模型类的实例作为从 UIKit 到 SwiftUI 通信的引用。在下面找到带有方法演示的修改代码。

使用 Xcode 12.4/iOS 14.4 测试

class ViewModel: ObservableObject {
@Published var name = "Name"
}

class ViewController: UIViewController {

private var vm: ViewModel!

override func viewDidLoad() {
super.viewDidLoad()

view.backgroundColor = UIColor.secondarySystemBackground

/// add the button
let button = UIButton()
button.frame = CGRect(x: 50, y: 50, width: 200, height: 100)
button.setTitle("Change name", for: .normal)
button.setTitleColor(.blue, for: .normal)
button.addTarget(self, action: #selector(handleTap), for: .touchUpInside)
view.addSubview(button)

/// add the SwiftUI ContentView
self.vm = ViewModel()
let contentView = ContentView(vm: self.vm)
let hostingController = UIHostingController(rootView: contentView)

addChild(hostingController)
view.insertSubview(hostingController.view, at: 0)
hostingController.view.frame = CGRect(x: 0, y: 400, width: view.bounds.width, height: view.bounds.height - 400)
hostingController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
hostingController.didMove(toParent: self)
}

@objc func handleTap() {
vm.name = "Updated name" /// update the name
}
}

struct ContentView: View {
@ObservedObject var vm: ViewModel

var body: some View {
Text(vm.name)
}
}

关于ios - 嵌入在 UIViewController 中的 UIHostingController - 如何从外部更新 @State?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66973097/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com