简体   繁体   English

使用 UIHostingController 将值从 SwiftUI 传递到 UIViewController

[英]Passing Value from SwiftUI to UIViewController Using UIHostingController

I have a SwiftUI view, which consists of a TextField.我有一个 SwiftUI 视图,它由一个 TextField 组成。 I want that whenever I type in the TextField it should send the value to the control on the UIKit UIViewController.我希望每当我输入 TextField 时,它都应该将值发送到 UIKit UIViewController 上的控件。

// Here is the ContentView 
class ContentViewDelegate: ObservableObject {

    var didChange = PassthroughSubject<ContentViewDelegate, Never>()

    var name: String = "" {
        didSet {
            self.didChange.send(self)
        }
    }
}

struct ContentView: View {

    @ObservedObject var delegate: ContentViewDelegate

    init(delegate: ContentViewDelegate) {
        self.delegate = delegate
    }

    var body: some View {
        VStack {
            TextField("Enter name", text: self.$delegate.name)
                .textFieldStyle(RoundedBorderTextFieldStyle())
            }.padding()
        .background(Color.green)
    }
}

I checked didChange does get fired in the above code.我检查了 didChange 确实在上面的代码中被解雇了。 But in the code below, the sink is never fired.但是在下面的代码中,sink 永远不会被触发。

class ViewController: UIViewController {

    private var delegate = ContentViewDelegate()
    private var contentView: ContentView!

    override func viewDidLoad() {
        super.viewDidLoad()

        self.contentView = ContentView(delegate: self.delegate)

        let controller = UIHostingController(rootView: self.contentView)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        self.addChild(controller)
        self.view.addSubview(controller.view)
        controller.didMove(toParent: self)

        NSLayoutConstraint.activate([
            controller.view.widthAnchor.constraint(equalToConstant: 200),
            controller.view.heightAnchor.constraint(equalToConstant: 44),
            controller.view.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            controller.view.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
        ])

        _ = self.delegate.didChange.sink { delegate in
            print(delegate.name)
        }

    }

Any ideas why didChange.sink is not getting fired?任何想法为什么 didChange.sink 没有被解雇?

If you assign the publisher to _ then it is deallocated when viewDidLoad returns.如果您将发布者分配给_ ,则它会在viewDidLoad返回时被释放。 Early examples from Apple show the assignment to _ and it used to work. Apple 的早期示例显示了对_的分配,并且它曾经有效。

You need to ensure you keep a strong reference to your publisher using a property:您需要确保使用属性保持对发布者的强引用:

class ViewController: UIViewController {

    private var delegate = ContentViewDelegate()
    private var contentView: ContentView!
    private var textChangePublisher: AnyCancellable?

    override func viewDidLoad() {
        super.viewDidLoad()

        self.contentView = ContentView(delegate: self.delegate)

        let controller = UIHostingController(rootView: self.contentView)
        controller.view.translatesAutoresizingMaskIntoConstraints = false
        self.addChild(controller)
        self.view.addSubview(controller.view)
        controller.didMove(toParent: self)

        NSLayoutConstraint.activate([
            controller.view.widthAnchor.constraint(equalToConstant: 200),
            controller.view.heightAnchor.constraint(equalToConstant: 44),
            controller.view.centerXAnchor.constraint(equalTo: self.view.centerXAnchor),
            controller.view.centerYAnchor.constraint(equalTo: self.view.centerYAnchor)
        ])

        self.textChangePublisher = self.delegate.didChange.sink { delegate in
            print(delegate.name)
        }

    }

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

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