0

Could someone please outline what exactly this line does?

    [self.delegate calendar:self didSelectDate:self.selectedDate];

This line basically is used to set the date on a label dateLabel which is in another class.

    CKCalendarView *calendar = [[CKCalendarView alloc] initWithStartDay:startMonday];
    calendar.delegate = self;

    self.dateFormatter = [[NSDateFormatter alloc] init];
    [self.dateFormatter setDateFormat:@"dd/MM/yyyy"];
    calendar.selectedDate = [self.dateFormatter dateFromString:@"18/11/2012"];
    calendar.minimumDate = [self.dateFormatter dateFromString:@"09/11/2012"];
    calendar.maximumDate = [self.dateFormatter dateFromString:@"29/11/2012"];

    calendar.frame = CGRectMake(10, 10, 300, 320);
    [self.view addSubview:calendar];

    self.dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, CGRectGetMaxY(calendar.frame) + 4, self.view.bounds.size.width, 24)];
    [self.view addSubview:self.dateLabel];

A 'couple line' detailed explanation on what the above line does would be of great help.

dev
  • 900
  • 2
  • 12
  • 26

1 Answers1

2

Briefly, your calendar has an instance of a CKCalendar object, a view that looks to be a subclass of one of the UIView classes (i'm assuming, because we can't tell since you didn't include an .h file or complete code from this .m file you're working with).

Most of the classes descended from UIView require a delegate to handle behaviors in, and generated by, the user interface. (For a good description of what delegates do, see How does a delegate work in objective-C?). Note, you explicitly set the delegate right after you instantiated calendar:

calendar.delegate = self

So with that line, you made this class both the display and the delegate so your class needs to implement whatever methods the protocol requires (one of which looks to be -(void)calendar:didSelectDate:.)

The line of code you quote says (basically) "Use the -(void)calendar:didSelectDate: method found in the delegate and pass itself and the selectedDate as the parameter/arguments for that method.".

I'd have written that line as:

[[self delegate] calendar:self didSelectDate:[self selectedDate]];

Hope that helps.

Community
  • 1
  • 1
Tony Armstrong
  • 638
  • 7
  • 17
  • 2
    In Objective-C, the method name would actually be `-calendar:didSelectDate:`, it's not quite accurate to call `calendar:` and `didSelecteDate:` two methods as you did in paragraph 3. Instead, there is a single delegate method `-calendar:didSelectDate:` which takes two parameters, the calendar, and the date. Other than that, it's a good description. – gaige Nov 18 '12 at 13:25
  • You're correct @gaige. I should have made that more clear. I'll edit my answer to reflect that. While I didn't want to convey incorrect information to the OP, I wanted to try to be very clear in explaining the delegate situation for them. In doing so, I wasn't so clear on the correct reading of the called method. Thank you for the assist! – Tony Armstrong Nov 18 '12 at 15:20