我如何从 Coordinator 访问 ViewController Class?
How can i access ViewController Class from Coordinator?
我在我的 SwiftUI 中使用 UIViewControllerRepresentable
Class,如下所示:
struct CalendarDayView: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
func makeUIViewController(context: Context) -> DayViewController {
let dayView = DayViewController()
dayView.delegate = context.coordinator
return dayView
}
func updateUIViewController(_ dayView: DayViewController, context: Context) {
dayView.delegate = context.coordinator
}
typealias UIViewControllerType = DayViewController
class Coordinator: NSObject, DayViewDelegate {
var parent: CalendarDayView
init(_ parent: CalendarDayView) {
self.parent = parent
}
func dayViewDidSelectEventView(_ eventView: EventView) {
guard let ckEvent = eventView.descriptor as? EKWrapper else { return }
let ekEvent = ckEvent.ekEvent
endEventEditing() // Cannot find 'endEventEditing' in scope
}
}
}
在 dayViewDidSelectEventView()
函数中,我需要访问来自 DayViewController Class 的 endEventEditing()
函数。我已经尝试将 parent 传递给协调器,但这没有帮助。我无法像这样通过 parent 变量访问 endEventEditing() 函数:
parent.endEventEditing() // Value of type 'CalendarDayView' has no member 'endEventEditing'
那么有没有办法访问 DayViewController
Class,由 Coordinator 中的 makeUIViewController
函数生成?
我可以 use/pass dayView
变量
let dayView = DayViewController()
我的协调器,以便我可以使用来自此控制器的功能?
您可以将它注入协调器的 属性(使用它 weak
以避免交叉引用)。
这是一个演示:
struct CalendarDayView: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
func makeUIViewController(context: Context) -> DayViewController {
let dayView = DayViewController()
dayView.delegate = context.coordinator
context.coordinator.viewController = dayView // << inject !!
return dayView
}
func updateUIViewController(_ dayView: DayViewController, context: Context) {
dayView.delegate = context.coordinator
}
typealias UIViewControllerType = DayViewController
class Coordinator: NSObject, DayViewDelegate {
weak var viewController: DayViewController? // << declare here !!
var parent: CalendarDayView
init(_ parent: CalendarDayView) {
self.parent = parent
}
func dayViewDidSelectEventView(_ eventView: EventView) {
guard let ckEvent = eventView.descriptor as? EKWrapper else { return }
let ekEvent = ckEvent.ekEvent
viewController?.endEventEditing() // << call !!
}
}
}
我在我的 SwiftUI 中使用 UIViewControllerRepresentable
Class,如下所示:
struct CalendarDayView: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
func makeUIViewController(context: Context) -> DayViewController {
let dayView = DayViewController()
dayView.delegate = context.coordinator
return dayView
}
func updateUIViewController(_ dayView: DayViewController, context: Context) {
dayView.delegate = context.coordinator
}
typealias UIViewControllerType = DayViewController
class Coordinator: NSObject, DayViewDelegate {
var parent: CalendarDayView
init(_ parent: CalendarDayView) {
self.parent = parent
}
func dayViewDidSelectEventView(_ eventView: EventView) {
guard let ckEvent = eventView.descriptor as? EKWrapper else { return }
let ekEvent = ckEvent.ekEvent
endEventEditing() // Cannot find 'endEventEditing' in scope
}
}
}
在 dayViewDidSelectEventView()
函数中,我需要访问来自 DayViewController Class 的 endEventEditing()
函数。我已经尝试将 parent 传递给协调器,但这没有帮助。我无法像这样通过 parent 变量访问 endEventEditing() 函数:
parent.endEventEditing() // Value of type 'CalendarDayView' has no member 'endEventEditing'
那么有没有办法访问 DayViewController
Class,由 Coordinator 中的 makeUIViewController
函数生成?
我可以 use/pass dayView
变量
let dayView = DayViewController()
我的协调器,以便我可以使用来自此控制器的功能?
您可以将它注入协调器的 属性(使用它 weak
以避免交叉引用)。
这是一个演示:
struct CalendarDayView: UIViewControllerRepresentable {
func makeCoordinator() -> Coordinator {
return Coordinator(self)
}
func makeUIViewController(context: Context) -> DayViewController {
let dayView = DayViewController()
dayView.delegate = context.coordinator
context.coordinator.viewController = dayView // << inject !!
return dayView
}
func updateUIViewController(_ dayView: DayViewController, context: Context) {
dayView.delegate = context.coordinator
}
typealias UIViewControllerType = DayViewController
class Coordinator: NSObject, DayViewDelegate {
weak var viewController: DayViewController? // << declare here !!
var parent: CalendarDayView
init(_ parent: CalendarDayView) {
self.parent = parent
}
func dayViewDidSelectEventView(_ eventView: EventView) {
guard let ckEvent = eventView.descriptor as? EKWrapper else { return }
let ekEvent = ckEvent.ekEvent
viewController?.endEventEditing() // << call !!
}
}
}