![](/img/trans.png)
[英]SwiftUI - @Published property is not updating view from coordinator
[英]Pass @Published properties from view controllers to SwiftUI
假设您有一个我想与 SwiftUI 一起使用的旧视图控制器。 视图控制器有一个包含当前状态的 @Published 属性:
class LegacyViewController: UIViewController {
enum State {
case opened
case closed
case halfOpened
}
@Published var state: State
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
self.state = .closed
super.init(nibName: nil, bundle: nil)
}
override func viewDidLoad() {
super.viewDidLoad()
// state is changed after some time
}
}
理想情况下,我想像这样在 SwiftUI 中使用它:
struct ContentView: View {
@State var state: LegacyViewController.State
var body: some View {
VCWrapper(state: $state).overlay (
Text("\(state)")
)
}
}
这意味着我需要实现UIViewControllerRepresentable
协议:
struct VCWrapper: UIViewControllerRepresentable {
@Binding var state: LegacyViewController.State
func makeUIViewController(context: Context) -> LegacyViewController {
let vc = LegacyViewController(nibName: nil, bundle: nil)
/// where to perform the actual binding?
return vc
}
func updateUIViewController(_ uiViewController: LegacyViewController, context: Context) {
}
}
然而,我无法搞清楚在哪里做,从实际的结合state
的财产LegacyViewController
的state
通过公开的属性VCWrapper
。 如果LegacyViewController
公开了一个委托,我可以通过Coordinator
对象实现绑定,但考虑到我不使用委托对象,我不太确定如何做到这一点?
这是可能的解决方案 - 使用Combine
。 使用 Xcode 12 / iOS 14 测试。
import Combine
struct VCWrapper: UIViewControllerRepresentable {
@Binding var state: LegacyViewController.State
func makeUIViewController(context: Context) -> LegacyViewController {
let vc = LegacyViewController(nibName: nil, bundle: nil)
// subscribe to controller state publisher and update bound
// external state
context.coordinator.cancelable = vc.$state
.sink {
DispatchQueue.main.async {
_state.wrappedValue = $0
}
}
return vc
}
func updateUIViewController(_ uiViewController: LegacyViewController, context: Context) {
}
func makeCoordinator() -> Coordinator {
Coordinator()
}
class Coordinator {
var cancelable: AnyCancellable?
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.