将绑定传递给SwiftUI视图的UIKit UIViewController

时间:2020-10-28 16:05:16

标签: ios swift swiftui

我正在尝试在UIKit中提供SwiftUI View。当前,有一个Binding<Bool>用于关闭/显示DetailView。这是工作代码:

struct ContentView: View {
    @State var presentingModal = false
    var body: some View {
        Button("Present") { self.presentingModal = true } /// tap button to present detail sheet
        .sheet(isPresented: $presentingModal) {
            DetailView(isPresentedBinding: $presentingModal) /// pass in the binding
        }
    }
}

struct DetailView: View {
    var isPresented: Binding<Bool>? /// it's an optional so I do this instead of `@Binding var isPresented: Bool`
    var body: some View {
        Button("Done") { isPresented?.wrappedValue = false } /// tap the "Done" button to dismiss
    }
}

当您按下“完成”按钮时,它将isPresented设置为false,这将关闭工作表。现在,我要展示DetailView中的UIViewController。这是我到目前为止所拥有的:

class ViewController: UIViewController {
    @IBAction func presentButtonPressed(_ sender: Any) {
        let detailViewController = DetailView_UIKit()
        self.present(detailViewController, animated: true, completion: nil)
    }
}

/// wrap DetailView in a ViewController so it's easier to present
class DetailView_UIKit: UIViewController {
    
    init() {
        super.init(nibName: nil, bundle: nil)
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func loadView() {
        
        view = UIView()

        /// Host the `DetailView`
        let detailViewController = UIHostingController(
            rootView: DetailView() /// I need to pass a Binding in here!
        )
        
        self.addChild(detailViewController)
        view.addSubview(detailViewController.view)
        detailViewController.view.frame = view.bounds
        detailViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        detailViewController.didMove(toParent: self)
    }
    
    /// I want to call this function too
    func dismissThisViewController() {
        self.dismiss(animated: true, completion: nil)
    }
}

这有效,但是“完成”按钮没有任何作用... Binding仍然为零,因为我没有将其分配给任何东西。如何在@State内创建某种DetailView_UIKit属性,以便可以将其传递到DetailView中?我希望在该属性设置为dismissThisViewController时调用false。不确定,也许创建@State属性不是正确的方法。

SwiftUI流程:

ContentView (present)→ DetailView
        ◟________________◞
             Binding

UIKit流程:

ViewController (present)→ DetailView_UIKit → DetailView
                               ◟________________◞
                          Binding? Not sure what to put

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

事实证明,您不能在BindingUIViewController之间使用View

最后,我在DetailView中做了一个可选的闭包。

  • 如果我展示DetailView_UIKit,我会将闭包分配给dismiss函数,但传递Binding(您还是不能)。
  • 如果我直接出示DetailView,我将像以前一样传递Binding,但分配闭包。

然后,当我按下DetailView内的按钮时,我会尝试

  1. Binding设置为false
  2. 致电关闭

顺序无关紧要,因为其中之一永远为零。

DetailView

struct DetailView: View {
    var isPresented: Binding<Bool>? /// Optional `Binding` from before

    var donePressed: (() -> Void)? /// The optional closure I just added

    var body: some View {
        Button("Done") {

            /// Attempt to set the `Binding` (dismiss the `.sheet` if SwiftUI)
            isPresented?.wrappedValue = false

            /// Attempt to call the closure (call the `self.dismiss` if UIKit)
            donePressed?()

        }
    }
}

DetailView_UIKit

override func loadView() {
    
    view = UIView()
    
    /// Host the `DetailView`
    let detailViewController = UIHostingController(
    rootView: DetailView() /// I DO NOT pass in a `Binding` here!
    )
    
    /// Assign the closure!
    detailViewController.donePressed = { [weak self] in
        self?.dismissThisViewController()
    }
    
    self.addChild(detailViewController)
    view.addSubview(detailViewController.view)
    detailViewController.view.frame = view.bounds
    detailViewController.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
    detailViewController.didMove(toParent: self)
}

/// Call this function in the closure
func dismissThisViewController() {
    self.dismiss(animated: true, completion: nil)
}