我想创建一个类似iOS默认日历的应用。我有屏幕的两个侧面。顶部是containerView
和pageviewcontroller
,用户可以从中选择星期几。底部是一个containerView
和一个pageviewcontroller
,那里是一张每天都有东西的桌子。
在topContainerView
中,我创建了一个pageViewController
函数,该函数创建一个具有7天和每天按钮的视图控制器。
我想要有人点击。在pageViewController
之后和之前更改视图控制器的一天。我为每个星期的控制器做了一个协议委托,当点击一天时,星期页面控制器会改变日期,但是前后的视图控制器都不会改变。
class WeekPageController: UIPageViewController {
// MARK: - IBOutlets & vars -
var currentDate = Date()
var presentedController: WeekController!
weak var calendarDelegate: CalendarDelegate?
// MARK: - IBActions & fat -
private func getPage(data: Date) -> WeekController? {
guard let page = storyboard?.instantiateViewController(withIdentifier: CConstants.CONTROLLER.WEEK) as? WeekController else { return nil }
page.currentDate = data
page.delegate = self
return page
}
// MARK: - Lifecycle -
override func viewDidLoad() {
super.viewDidLoad()
automaticallyAdjustsScrollViewInsets = true
dataSource = self
delegate = self
presentedController = getPage(data: currentDate)
presentedController?.afisare(data: currentDate)
setViewControllers([presentedController!], direction: .forward, animated: true, completion: nil)
}
}
extension WeekPageController: PageViewControllerDelegate {
func ziSaptamana(_ ziSaptamana : Int)
{
var weekDay = Calendar.current.component(.weekday, from: currentDate)
if weekDay == 1 { weekDay = 7 } else { weekDay -= 1}
if weekDay != 7 {
currentDate = Calendar.current.date(byAdding: .day, value: (ziSaptamana - weekDay), to: currentDate)!
} else {
currentDate = Calendar.current.date(byAdding: .day, value: ziSaptamana - 7, to: currentDate)!
}
self.presentedController?.afisare(data: currentDate)
}
extension WeekPageController: UIPageViewControllerDataSource {
func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
let day = (viewController as! WeekController).currentDate
guard var dayBefore = Calendar.current.date(byAdding: .day, value: -7, to: day) else { return nil }
return getPage(data: dayBefore)
}
func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
let day = (viewController as! WeekController).currentDate
guard var dayAfter = Calendar.current.date(byAdding: .day, value: +7, to: day) else { return nil }
return getPage(data: dayAfter)
}
}
extension WeekPageController: UIPageViewControllerDelegate {
func pageViewController(_ pageViewController: UIPageViewController, willTransitionTo pendingViewControllers: [UIViewController]) {
self.presentedController = pendingViewControllers.first as! WeekController
self.presentedController.afisare(data: presentedController.currentDate)
}
func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
self.currentDate = (self.viewControllers?.first as! WeekController).currentDate
}
protocol PageViewControllerDelegate : class {
func ziSaptamana(_: Int)
}
class WeekController: UIViewController {
var index: Int = 0
var currentDate = Date()
weak var delegate: PageViewControllerDelegate?
private func delegates(id: Int){
delegate?.ziSaptamana(id)
}
@IBAction func luni(_ sender: UIButton) {
delegates(id: 1)
}
}
func afisare(data: Date) {
}
}