【发布时间】:2021-02-13 14:37:45
【问题描述】:
这就是我想要达到的目标:
class MyVC: UIViewController {
@State var myBoolState: Bool = false
private var subscribers = Set<AnyCancellable>()
override func viewDidLoad() {
super.viewDidLoad()
myBoolState.sink { value in .... }.store(in:&subscribers)
}
func createTheView() {
let vc = UIHostingController(rootView: MySwiftUIView(myBoolState: $myBoolState))
self.navigationController!.pushViewController(vc, animated: true)
}
}
struct MySwiftUIView: View {
@Binding var myBoolState: Bool
var body: some View {
Button(action: {
myBoolState = true
}) {
Text("Push Me")
}
}
}
但是上面的当然不能编译。
所以问题是:我能否以某种方式在视图控制器中声明已发布的属性,将其传递给 SwiftUI 视图并在 SwiftUI 视图更改其值时得到通知?
【问题讨论】: