我正在尝试实现需要委托方法的功能(例如NSUserActivity
)。因此,我需要一个符合UIViewController
(或类似的其他委托人),处理并保存所有必需信息的NSUserActivityDelegate
。我的问题是我在界面上使用的是SwiftUI,因此我没有在使用UIViewControllers
。因此,我该如何实现此功能,同时仍将SwiftUI用于UI。我尝试了什么:view1只是一个普通的SwiftUI View
,可以显示(通过NavigationLink
)view2,view2是要实现此功能的视图。因此,我尝试代替链接view1和view2,而是将view1链接到UIViewControllerRepresentable
,然后UIHostingController(rootView: view2)
处理此功能的实现,并将struct view1: View {
var body: some View {
NavigationLink(destination: VCRepresentable()) {
Text("Some Label")
}
}
}
struct view2: View {
var body: some View {
Text("Hello World!")
}
}
struct VCRepresentable: UIViewControllerRepresentable {
func makeUIViewController(context: Context) -> UIViewController {
return implementationVC()
}
func updateUIViewController(_ uiViewController: UIViewController, context: Context) { }
}
class implementationVC: UIViewController, SomeDelegate for functionality {
// does implementation stuff in delegate methods
...
override func viewDidLoad() {
super.viewDidLoad()
attachChild(UIHostingController(rootView: view2()))
}
private func attachChild(_ viewController: UIViewController) {
addChild(viewController)
if let subview = viewController.view {
subview.translatesAutoresizingMaskIntoConstraints = false
view.addSubview(subview)
subview.widthAnchor.constraint(equalTo: view.widthAnchor).isActive = true
subview.heightAnchor.constraint(equalTo: view.heightAnchor).isActive = true
subview.centerXAnchor.constraint(equalTo: view.centerXAnchor).isActive = true
subview.centerYAnchor.constraint(equalTo: view.centerYAnchor).isActive = true
}
viewController.didMove(toParent: self)
}
}
添加为子视图控制器。
{{1}}
我在VC和view2之间传输数据时遇到麻烦。因此,我想知道是否有更好的方法可以在SwiftUI视图中实现这种功能。
答案 0 :(得分:2)
您需要创建一个符合UIViewControllerRepresentable
并具有Coordinator
的视图,该视图可以处理所有委托功能。
例如,使用示例视图控制器和委托:
struct SomeDelegateObserver: UIViewControllerRepresentable {
let vc = SomeViewController()
var foo: (Data) -> Void
func makeUIViewController(context: Context) -> SomeViewController {
return vc
}
func updateUIViewController(_ uiViewController: SomeViewController, context: Context) { }
func makeCoordinator() -> Coordinator {
Coordinator(foo: foo)
}
class Coordinator: NSObject, SomeDelegate {
var foo: (Data) -> Void
init(vc: SomeViewController, foo: @escaping (Data) -> Void) {
self.foo = foo
super.init()
vc.delegate = self
}
func someDelegateFunction(data: Data) {
foo()
}
}
}
用法:
struct ContentView: View {
var dataModel: DataModel
var body: some View {
NavigationLink(destination: CustomView(numberFromPreviousView: 10)) {
Text("Go to VCRepresentable")
}
}
}
struct CustomView: View {
@State var instanceData1: String = ""
@State var instanceData2: Data?
var numberFromPreviousView: Int // example of data passed from the previous view to this view, the one that can react to the delegate's functions
var body: some View {
ZStack {
SomeDelegateObserver { data in
print("Some delegate function was executed.")
self.instanceData1 = "Executed!"
self.instanceData2 = data
}
VStack {
Text("This is the UI")
Text("That, in UIKit, you would have in the UIViewController")
Text("That conforms to whatever delegate")
Text("SomeDelegateObserver is observing.")
Spacer()
Text(instanceData1)
}
}
}
}
注意:我将VCRepresentable
重命名为SomeDelegateObserver
是为了更好地表明它的作用:其唯一目的是等待委托函数执行然后运行闭包(即本示例中的foo
)由您提供。您可以使用此模式创建所需数量的功能,以“观察”您关心的任何委托功能,然后执行可更新UI,数据模型等的代码。在我的示例中,当SomeDelegate
触发someDelegateFunction(data:)
,该视图将显示“已执行”并更新数据实例变量。