patchthecode / JTAppleCalendar

The Unofficial Apple iOS Swift Calendar View. Swift calendar Library. iOS calendar Control. 100% Customizable
https://patchthecode.com
MIT License
7.57k stars 812 forks source link

Targetting self for dataSource and delegates #46

Closed yillivs closed 8 years ago

yillivs commented 8 years ago

Hello,

I seem to have an issue on the last real step of the tutorial, which is setting up the dataSource and delegates. I try to set them both to self as is shown in the tutorial and I get an error that "Cannot assign value of type monthViewController(My view handling class name) to JTAppleCalendarDataSource". This is what the troubling section of code looks like.

import JTAppleCalendar

class MonthViewController: UIViewController {

    //MARK: Properties
    @IBOutlet weak var calendarView: JTAppleCalendarView!

    //Default UIViewController Methods:
    override func viewDidLoad() {
        super.viewDidLoad()

        calendarView.dataSource = self
        calendarView.delegate = self

        calendarView.registerCellViewXib(fileName: "CellView") // Registering your cell is manditory
    }
patchthecode commented 8 years ago

Does your issue look something like this?

screen shot 2016-06-12 at 5 38 36 pm
yillivs commented 8 years ago

Yes exactly like that.

patchthecode commented 8 years ago

If it looks like that then, did you follow the step in the tutorial which has this code?

extension ViewController: JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate  {
    // Setting up manditory protocol method 
    func configureCalendar(calendar: JTAppleCalendarView) -> (startDate: NSDate, endDate: NSDate, numberOfRows: Int, calendar: NSCalendar) {
        // You can set your date using NSDate() or NSDateFormatter. Your choice.
        let formatter = NSDateFormatter()
        formatter.dateFormat = "yyyy MM dd"

        let firstDate = formatter.dateFromString("2016 01 05") 
        let secondDate = NSDate()
        let numberOfRows = 6
        let aCalendar = NSCalendar.currentCalendar() // Properly configure your calendar to your time zone here

        return (startDate: firstDate!, endDate: secondDate, numberOfRows: numberOfRows, calendar: aCalendar)
    }
}
patchthecode commented 8 years ago

Remember, your calendar has to conform to the dataSource protocol. If it does not conform to the datasource, XCode will scream at you :)

yillivs commented 8 years ago

I believe so, I wrote this in the class same class as that manages the calendar view.

func configureCalendar(calendar: JTAppleCalendarView) -> (startDate: NSDate, endDate: NSDate, numberOfRows: Int, calendar: NSCalendar){

    let formatter = NSDateFormatter()//access to date formatter method
    formatter.dateFormat = "yyyy MM dd"

    let firstDate = formatter.dateFromString("2016 05 10")
    let secondDate = NSDate()
    let numberOfRows = 6

    let aCalendar = NSCalendar.currentCalendar()
    //return logical calendar for current user

    return(startDate: firstDate!, endDate: secondDate, numberOfRows: numberOfRows, calendar: aCalendar)
}
yillivs commented 8 years ago

But examining it currently I notice that I missed this line.

extension ViewController: JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate

Would that have caused this to occur?

patchthecode commented 8 years ago

Yes. That is what caused the issue.

You should either have this:

class MonthViewController: UIViewController, JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate { 
...
...
...
}

Or this

extension ViewController: JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate 

Doing that tells the calendar that:

Yes, i conform to your required protocol

If you leave it out, then the calendar does not know that you conform to the protocol and XCode will scream at you.

It is your choice as to where you want to put it. I like putting mine in the extension to keep my code neat. Once you insert that missing line, your error should go away

patchthecode commented 8 years ago

Let me know if your issue is resolved. And if it is, then you can close this issue 👍

yillivs commented 8 years ago

That fixed it!

patchthecode commented 8 years ago

awesome! Now go build a extremely cool looking calendar and post an image! have a good one.