john doe
john doe

Reputation: 9660

Passing Value from SwiftUI to UIViewController Using UIHostingController

I have a SwiftUI view, which consists of a TextField. I want that whenever I type in the TextField it should send the value to the control on the 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. But in the code below, the sink is never fired.

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?

Upvotes: 1

Views: 1535

Answers (1)

Paulw11
Paulw11

Reputation: 114836

If you assign the publisher to _ then it is deallocated when viewDidLoad returns. Early examples from Apple show the assignment to _ and it used to work.

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

    }

Upvotes: 7

Related Questions