JTAppleCalendar icon indicating copy to clipboard operation
JTAppleCalendar copied to clipboard

Targetting self for dataSource and delegates

Open yillivs opened this issue 10 years ago • 10 comments

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
    }

yillivs avatar Jun 12 '16 23:06 yillivs

Does your issue look something like this?

screen shot 2016-06-12 at 5 38 36 pm

patchthecode avatar Jun 13 '16 00:06 patchthecode

Yes exactly like that.

yillivs avatar Jun 13 '16 00:06 yillivs

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 avatar Jun 13 '16 00:06 patchthecode

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

patchthecode avatar Jun 13 '16 00:06 patchthecode

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 avatar Jun 13 '16 00:06 yillivs

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

extension ViewController: JTAppleCalendarViewDataSource, JTAppleCalendarViewDelegate

Would that have caused this to occur?

yillivs avatar Jun 13 '16 00:06 yillivs

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 avatar Jun 13 '16 00:06 patchthecode

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

patchthecode avatar Jun 13 '16 00:06 patchthecode

That fixed it!

yillivs avatar Jun 13 '16 01:06 yillivs

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

patchthecode avatar Jun 13 '16 01:06 patchthecode