更新到 8.0 后,JTAppleCalendar 无法触发 didSelectDate
JTAppleCalendar fails to trigger didSelectDate after update to 8.0
我正在使用 JTAppleCalendar 将日历添加到我的应用程序中的视图。我一切正常,但在我更新到 8.0 后,我无法 link 控制器正常工作。
这是我得到的错误
Assertion failed: Attemped to access calendar when Datasource/delegate
is not set yet. Returning default value: file
/Users/studio/Desktop/my_app/Pods/JTAppleCalendar/Sources/JTAppleCalendar/JTACVariables.swift, line 109 2019-09-23 00:51:42.437357+0300 Ways[4044:97207] Assertion
failed: Attemped to access calendar when Datasource/delegate is not
set yet. Returning default value: file
/Users/studio/Desktop/my_app/Pods/JTAppleCalendar/Sources/JTAppleCalendar/JTACVariables.swift, line 109
这是我的控制器
import UIKit
import JTAppleCalendar
class LandingPageViewController: UIViewController {
let formatter = DateFormatter()
var queryID: String = ""
var targetDate: Date?
var dateSelected: Date = Date()
var timeSelected: Date = Date()
@IBOutlet weak var calendarView: JTACMonthView!
@IBOutlet weak var yearLabel: UILabel!
@IBAction func calendarSaveButton(_ sender: Any) {
if let finalDate = combineDateWithTime(date: dateSelected, time: timeSelected) {
QueryModel().addTargetDate(id: queryID, targetDate: finalDate)
dismiss(animated: true, completion: nil)
}
}
func combineDateWithTime(date: Date, time: Date) -> Date? {
let calendar = NSCalendar.current
let dateComponents = calendar.dateComponents([.year, .month, .day], from: date)
let timeComponents = calendar.dateComponents([.hour, .minute, .second], from: time)
var mergedComponments = DateComponents()
mergedComponments.year = dateComponents.year!
mergedComponments.month = dateComponents.month!
mergedComponments.day = dateComponents.day!
mergedComponments.hour = timeComponents.hour!
mergedComponments.minute = timeComponents.minute!
mergedComponments.second = timeComponents.second!
return calendar.date(from: mergedComponments)
}
override func viewDidLoad() {
super.viewDidLoad()
setupCalendarView()
formatter.timeZone = Calendar.current.timeZone
formatter.locale = Calendar.current.locale
self.calendarView.scrollToDate(dateSelected, animateScroll: false)
self.calendarView.selectDates([dateSelected])
}
@objc func handleDatePicker(sender: UIDatePicker) {
timeSelected = sender.date;
}
func setupCalendarView(){
calendarView.minimumLineSpacing = 0
calendarView.minimumInteritemSpacing = 0
calendarView.visibleDates { (visibleDates) in
self.setupViewsFromCalendar(from: visibleDates)
}
}
func setupViewsFromCalendar(from: DateSegmentInfo){
let date = from.monthDates.first!.date
formatter.dateFormat = "yyyy"
yearLabel.text = formatter.string(from: date)
formatter.dateFormat = "MMMM"
}
func handleCellSelection(myCustomCell: CalendarCell, cellState: CellState, date: Date) {
myCustomCell.dateLabel.text = cellState.text
if cellState.isSelected {
dateSelected = date
myCustomCell.selectedDay.isHidden = false
myCustomCell.selectedDay.backgroundColor = UIColor.orange
}else {
myCustomCell.selectedDay.isHidden = true
}
}
func handleCellTextColor(myCustomCell: CalendarCell, cellState: CellState, date: Date){
if cellState.isSelected{
myCustomCell.dateLabel.textColor = UIColor.white
}else{
if cellState.dateBelongsTo == .thisMonth {
myCustomCell.dateLabel.textColor = UIColor.black
}else{
myCustomCell.dateLabel.textColor = UIColor.gray
}
}
}
func handleTodayCell(myCustomCell: CalendarCell, cellState: CellState, date: Date){
formatter.dateFormat = "dd MM yyyy"
let todayDateString = formatter.string(from: Date())
let cellDateString = formatter.string(from: cellState.date);
if todayDateString == cellDateString {
// Checks to see if today is the current cell and apply different styling
myCustomCell.selectedDay.isHidden = false
myCustomCell.selectedDay.backgroundColor = UIColor.blue
myCustomCell.dateLabel.textColor = UIColor.white
}
}
}
extension LandingPageViewController:JTACMonthViewDataSource {
func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
let myCustomCell = cell as! CalendarCell
handleCellSelection(myCustomCell: myCustomCell, cellState: cellState, date: date)
}
func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {
let startDate = Date()
//print(startDate);
let yearsToAdd = 10
let endDate = Calendar.current.date(byAdding: .year, value: yearsToAdd, to: startDate)
let params = ConfigurationParameters(
startDate: startDate,
endDate: endDate!,
generateInDates: .forAllMonths,
generateOutDates: .tillEndOfRow
)
return params
}
}
extension LandingPageViewController: JTACMonthViewDelegate {
func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CalendarCell", for: indexPath) as! CalendarCell
handleCellSelection(myCustomCell: cell, cellState: cellState, date: date)
handleCellTextColor(myCustomCell: cell, cellState: cellState, date: date)
handleTodayCell(myCustomCell: cell, cellState: cellState, date: date)
return cell
}
func calendar(_ calendar: JTACMonthView, didSelectDate date: Date, cell: JTACDayCell?, cellState: CellState) {
guard let validCell = cell as? CalendarCell else { return }
handleCellSelection(myCustomCell: validCell, cellState: cellState, date: date)
handleCellTextColor(myCustomCell: validCell, cellState: cellState, date: date)
}
func calendar(_ calendar: JTACMonthView, didDeselectDate date: Date, cell: JTACDayCell?, cellState: CellState) {
guard let validCell = cell as? CalendarCell else { return }
handleCellSelection(myCustomCell: validCell, cellState: cellState, date: date)
handleCellTextColor(myCustomCell: validCell, cellState: cellState, date: date)
handleTodayCell(myCustomCell: validCell, cellState: cellState, date: date)
}
func calendar(_ calendar: JTACMonthView, shouldSelectDate date: Date, cell: JTACDayCell?, cellState: CellState) -> Bool {
if (cellState.date.timeIntervalSince(Date()) > -86400) {
return true
}else {
return false
}
}
func calendar(_ calendar: JTACMonthView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
setupViewsFromCalendar(from: visibleDates)
}
}
我刚刚将 class 名称更改为新版本。
我想你错过了 migration guide。在那里它告诉你重命名你的函数并给你一个要重命名的函数列表。
在您的 didSelect/didDeselect 情况下,应将其重命名为
func calendar(_ calendar: JTACMonthView, didSelectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath)
func calendar(_ calendar: JTACMonthView, didDeselectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath)
因为你没有重命名它们,所以它们没有被调用。请在需要的地方重命名其他人。
关于数据源错误,当您从 InterfaceBuilder 中删除 DataSource/Delegate 并重新连接它们时会发生什么(可能是 IB 搞砸了)?
我正在使用 JTAppleCalendar 将日历添加到我的应用程序中的视图。我一切正常,但在我更新到 8.0 后,我无法 link 控制器正常工作。
这是我得到的错误
Assertion failed: Attemped to access calendar when Datasource/delegate is not set yet. Returning default value: file /Users/studio/Desktop/my_app/Pods/JTAppleCalendar/Sources/JTAppleCalendar/JTACVariables.swift, line 109 2019-09-23 00:51:42.437357+0300 Ways[4044:97207] Assertion failed: Attemped to access calendar when Datasource/delegate is not set yet. Returning default value: file /Users/studio/Desktop/my_app/Pods/JTAppleCalendar/Sources/JTAppleCalendar/JTACVariables.swift, line 109
这是我的控制器
import UIKit
import JTAppleCalendar
class LandingPageViewController: UIViewController {
let formatter = DateFormatter()
var queryID: String = ""
var targetDate: Date?
var dateSelected: Date = Date()
var timeSelected: Date = Date()
@IBOutlet weak var calendarView: JTACMonthView!
@IBOutlet weak var yearLabel: UILabel!
@IBAction func calendarSaveButton(_ sender: Any) {
if let finalDate = combineDateWithTime(date: dateSelected, time: timeSelected) {
QueryModel().addTargetDate(id: queryID, targetDate: finalDate)
dismiss(animated: true, completion: nil)
}
}
func combineDateWithTime(date: Date, time: Date) -> Date? {
let calendar = NSCalendar.current
let dateComponents = calendar.dateComponents([.year, .month, .day], from: date)
let timeComponents = calendar.dateComponents([.hour, .minute, .second], from: time)
var mergedComponments = DateComponents()
mergedComponments.year = dateComponents.year!
mergedComponments.month = dateComponents.month!
mergedComponments.day = dateComponents.day!
mergedComponments.hour = timeComponents.hour!
mergedComponments.minute = timeComponents.minute!
mergedComponments.second = timeComponents.second!
return calendar.date(from: mergedComponments)
}
override func viewDidLoad() {
super.viewDidLoad()
setupCalendarView()
formatter.timeZone = Calendar.current.timeZone
formatter.locale = Calendar.current.locale
self.calendarView.scrollToDate(dateSelected, animateScroll: false)
self.calendarView.selectDates([dateSelected])
}
@objc func handleDatePicker(sender: UIDatePicker) {
timeSelected = sender.date;
}
func setupCalendarView(){
calendarView.minimumLineSpacing = 0
calendarView.minimumInteritemSpacing = 0
calendarView.visibleDates { (visibleDates) in
self.setupViewsFromCalendar(from: visibleDates)
}
}
func setupViewsFromCalendar(from: DateSegmentInfo){
let date = from.monthDates.first!.date
formatter.dateFormat = "yyyy"
yearLabel.text = formatter.string(from: date)
formatter.dateFormat = "MMMM"
}
func handleCellSelection(myCustomCell: CalendarCell, cellState: CellState, date: Date) {
myCustomCell.dateLabel.text = cellState.text
if cellState.isSelected {
dateSelected = date
myCustomCell.selectedDay.isHidden = false
myCustomCell.selectedDay.backgroundColor = UIColor.orange
}else {
myCustomCell.selectedDay.isHidden = true
}
}
func handleCellTextColor(myCustomCell: CalendarCell, cellState: CellState, date: Date){
if cellState.isSelected{
myCustomCell.dateLabel.textColor = UIColor.white
}else{
if cellState.dateBelongsTo == .thisMonth {
myCustomCell.dateLabel.textColor = UIColor.black
}else{
myCustomCell.dateLabel.textColor = UIColor.gray
}
}
}
func handleTodayCell(myCustomCell: CalendarCell, cellState: CellState, date: Date){
formatter.dateFormat = "dd MM yyyy"
let todayDateString = formatter.string(from: Date())
let cellDateString = formatter.string(from: cellState.date);
if todayDateString == cellDateString {
// Checks to see if today is the current cell and apply different styling
myCustomCell.selectedDay.isHidden = false
myCustomCell.selectedDay.backgroundColor = UIColor.blue
myCustomCell.dateLabel.textColor = UIColor.white
}
}
}
extension LandingPageViewController:JTACMonthViewDataSource {
func calendar(_ calendar: JTACMonthView, willDisplay cell: JTACDayCell, forItemAt date: Date, cellState: CellState, indexPath: IndexPath) {
let myCustomCell = cell as! CalendarCell
handleCellSelection(myCustomCell: myCustomCell, cellState: cellState, date: date)
}
func configureCalendar(_ calendar: JTACMonthView) -> ConfigurationParameters {
let startDate = Date()
//print(startDate);
let yearsToAdd = 10
let endDate = Calendar.current.date(byAdding: .year, value: yearsToAdd, to: startDate)
let params = ConfigurationParameters(
startDate: startDate,
endDate: endDate!,
generateInDates: .forAllMonths,
generateOutDates: .tillEndOfRow
)
return params
}
}
extension LandingPageViewController: JTACMonthViewDelegate {
func calendar(_ calendar: JTACMonthView, cellForItemAt date: Date, cellState: CellState, indexPath: IndexPath) -> JTACDayCell {
let cell = calendar.dequeueReusableJTAppleCell(withReuseIdentifier: "CalendarCell", for: indexPath) as! CalendarCell
handleCellSelection(myCustomCell: cell, cellState: cellState, date: date)
handleCellTextColor(myCustomCell: cell, cellState: cellState, date: date)
handleTodayCell(myCustomCell: cell, cellState: cellState, date: date)
return cell
}
func calendar(_ calendar: JTACMonthView, didSelectDate date: Date, cell: JTACDayCell?, cellState: CellState) {
guard let validCell = cell as? CalendarCell else { return }
handleCellSelection(myCustomCell: validCell, cellState: cellState, date: date)
handleCellTextColor(myCustomCell: validCell, cellState: cellState, date: date)
}
func calendar(_ calendar: JTACMonthView, didDeselectDate date: Date, cell: JTACDayCell?, cellState: CellState) {
guard let validCell = cell as? CalendarCell else { return }
handleCellSelection(myCustomCell: validCell, cellState: cellState, date: date)
handleCellTextColor(myCustomCell: validCell, cellState: cellState, date: date)
handleTodayCell(myCustomCell: validCell, cellState: cellState, date: date)
}
func calendar(_ calendar: JTACMonthView, shouldSelectDate date: Date, cell: JTACDayCell?, cellState: CellState) -> Bool {
if (cellState.date.timeIntervalSince(Date()) > -86400) {
return true
}else {
return false
}
}
func calendar(_ calendar: JTACMonthView, didScrollToDateSegmentWith visibleDates: DateSegmentInfo) {
setupViewsFromCalendar(from: visibleDates)
}
}
我刚刚将 class 名称更改为新版本。
我想你错过了 migration guide。在那里它告诉你重命名你的函数并给你一个要重命名的函数列表。
在您的 didSelect/didDeselect 情况下,应将其重命名为
func calendar(_ calendar: JTACMonthView, didSelectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath)
func calendar(_ calendar: JTACMonthView, didDeselectDate date: Date, cell: JTACDayCell?, cellState: CellState, indexPath: IndexPath)
因为你没有重命名它们,所以它们没有被调用。请在需要的地方重命名其他人。
关于数据源错误,当您从 InterfaceBuilder 中删除 DataSource/Delegate 并重新连接它们时会发生什么(可能是 IB 搞砸了)?