如何将@published 和@observed 与FSCalendar 一起使用?
Posted
技术标签:
【中文标题】如何将@published 和@observed 与FSCalendar 一起使用?【英文标题】:how to use @published and @observed with FSCalendar? 【发布时间】:2020-11-02 05:21:52 【问题描述】:如何从 FSCalendar 中获取例如 selectedDate?
struct CalendarController: UIViewControllerRepresentable
func makeCoordinator() -> Coordinator
Coordinator(self)
func makeUIViewController(context: UIViewControllerRepresentableContext<CalendarController>)
-> calendars
let calendarViewController = calendars()
return calendarViewController
func updateUIViewController(_ uiViewController: calendars, context:
UIViewControllerRepresentableContext<CalendarController>)
func updateUIViewController(_ uiViewController: CalendarController, context:
UIViewControllerRepresentableContext<CalendarController>)
class Coordinator: NSObject
var parent: CalendarController
init(_ calendarViewController: CalendarController)
self.parent = calendarViewController
class calendars: UIViewController, FSCalendarDelegate, ObservableObject
var calendar = FSCalendar()
@Published var selectedData : Date?
override func viewDidLoad()
super.viewDidLoad()
calendar.delegate = self
override func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()
calendar.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height:
view.frame.size.width)
view.addSubview(calendar)
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition:
FSCalendarMonthPosition)
print("didSelect date: \(date)")
selectedData = date
struct CalendarView: View
var body: some View
CalendarController().padding().frame(alignment: .top)
struct Calendar_Previews: PreviewProvider
static var previews: some View
CalendarView()
稍后在另一个快速视图中,我尝试获取日期,但它从未更改过。 我用的
@ObservedObject var calendarData = calendars()
//...
CalendarView().scaledToFit()
Text("\(self.calendarData.selectedData ?? Date())")
【问题讨论】:
我认为将视图控制器作为 ios 中的观察对象并不是一个好主意。在您的示例中,您正在代码中创建视图控制器的新实例,因此这不是您要更改的实例,因为我假设它是从情节提要创建的。 是swiftui。我尝试使用 FSCalendar - 在文档中,据说为此使用 UIViewControllerRepresentable 协议。我看到我创建了一个新实例。我没有足够的 iOS 开发经验来让它工作。 【参考方案1】:以下作品。
由于我是 iOS 新手,所以我很快就搞不懂委托的诀窍。
协调器类应实现它以使其工作。
''' 导入 SwiftUI 导入 UIKit 导入FSCalendar 导入合并
struct CalendarController: UIViewControllerRepresentable
@Binding var selectedDate : Date?
func makeCoordinator() -> CalendarController.Coordinator
Coordinator(self)
func makeUIViewController(context: Context) -> calendars
let calendar = calendars()
calendar.calendar.delegate = context.coordinator
return calendar
func updateUIViewController(_ uiViewController: calendars, context: Context)
class calendars: UIViewController
var calendar = FSCalendar()
var selectedDate : Date?
override func viewDidLoad()
super.viewDidLoad()
calendar.delegate = self // delegate
override func viewDidLayoutSubviews()
super.viewDidLayoutSubviews()
calendar.frame = CGRect(x: 0, y: 0, width: view.frame.size.width, height:
view.frame.size.width)
view.addSubview(calendar)
extension CalendarController
class Coordinator: NSObject, FSCalendarDelegate
var parent: CalendarController
init(_ parent: CalendarController)
self.parent = parent
func calendar(_ calendar: FSCalendar, didSelect date: Date, at
monthPosition: FSCalendarMonthPosition)
print(date)
parent.selectedDate = date
'''
【讨论】:
以上是关于如何将@published 和@observed 与FSCalendar 一起使用?的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI 和具有 @Published 的组合 Observable 对象不断发布