FSCalendar 具有“选择日期”委托功能。如果您正在查看 Swift Storyboard Sample,则会调用它并将格式化的日期打印到调试控制台。这就是你可以触发你的segue的地方。
首先添加一个“详细”视图控制器类:
//
// DetailViewController.swift
// FSCalendarSwiftExample
//
// Created by Don Mag on 8/31/20.
//
import UIKit
class DetailViewController: UIViewController {
var selectedDate: Date?
@IBOutlet var myLabel: UILabel!
fileprivate let formatter: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "yyyy/MM/dd"
return formatter
}()
override func viewDidLoad() {
super.viewDidLoad()
if let d = selectedDate {
myLabel.text = formatter.string(from: d)
}
}
}
向 Storyboard 添加一个带有几个标签的视图控制器,将其类设置为 DetailViewController,并将“详细标签”连接到 myLabel @IBOutlet:
按住 Ctrl 键从 FSCalendar 视图控制器顶部的视图控制器图标拖动到新的 Detail View Controller,然后从弹出菜单中选择 Show:
您会看到已添加segue。选择那个segue并给它一个标识符“gotoDetail”:
在InterfaceBuilderViewController类中,找到这个函数:
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
print("calendar did select date \(self.formatter.string(from: date))")
if monthPosition == .previous || monthPosition == .next {
calendar.setCurrentPage(date, animated: true)
}
}
并在末尾添加performSegue 行:
func calendar(_ calendar: FSCalendar, didSelect date: Date, at monthPosition: FSCalendarMonthPosition) {
print("calendar did select date \(self.formatter.string(from: date))")
if monthPosition == .previous || monthPosition == .next {
calendar.setCurrentPage(date, animated: true)
}
// add this line
self.performSegue(withIdentifier: "gotoDetail", sender: nil)
}
还在InterfaceBuilderViewController类中,找到prepare(for segue...函数:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let config = segue.destination as? CalendarConfigViewController {
config.lunar = self.lunar
config.theme = self.theme
config.selectedDate = self.calendar.selectedDate
config.firstWeekday = self.calendar.firstWeekday
config.scrollDirection = self.calendar.scrollDirection
}
}
并在末尾添加此代码:
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let config = segue.destination as? CalendarConfigViewController {
config.lunar = self.lunar
config.theme = self.theme
config.selectedDate = self.calendar.selectedDate
config.firstWeekday = self.calendar.firstWeekday
config.scrollDirection = self.calendar.scrollDirection
}
// add this code
if let detail = segue.destination as? DetailViewController {
detail.selectedDate = calendar.selectedDate
}
}
运行应用程序,从表格视图中选择Interface Builder,然后选择一个日期。您的新 Detail 控制器应该会被推送到视图中,并且应该会显示您选择的日期。