我正在尝试在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
属性不是正确的方法。
ContentView (present)→ DetailView
◟________________◞
Binding
ViewController (present)→ DetailView_UIKit → DetailView
◟________________◞
Binding? Not sure what to put
感谢您的帮助!
答案 0 :(得分:0)
Binding
和UIViewController
之间使用View
。最后,我在DetailView
中做了一个可选的闭包。
DetailView_UIKit
,我会将闭包分配给dismiss函数,但不传递Binding
(您还是不能)。DetailView
,我将像以前一样传递Binding
,但不分配闭包。然后,当我按下DetailView
内的按钮时,我会尝试
Binding
设置为false 顺序无关紧要,因为其中之一永远为零。
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)
}