使用UIHostingController

时间:2020-04-18 21:59:56

标签: ios swiftui

我有一个SwiftUI视图,其中包含一个TextField。我希望每当我在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)
    }
}

我检查didChange确实在上面的代码中被解雇了。但是在下面的代码中,接收器永远不会触发。

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

    }

任何想法为什么didChange.sink没有被解雇?

1 个答案:

答案 0 :(得分:5)

如果将发布者分配给_,则在viewDidLoad返回时将其释放。 Apple的早期示例显示了对_的分配,并且该分配曾经有效。

您需要确保使用属性对发布者保持强烈的引用:

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

    }