-1

Let me first say I didn't write this code; it was from the Calendar API written by tinyFool in 2010. As such, it did what it was supposed to do before Apple deprecated CFGregorianDate; now, I'm trying to convert it to the recommended NSCalendar. If you need more relevant code that I haven't supplied, let me know and I'll post it.

I have two statements that appear in several places:

    [calendarViewDelegate monthChanged: currentMonthDate viewLeftTop:self.frame.origin height:height];
    [calendarViewDelegate beforeMonthChange:self willto: currentMonthDate];

which are giving me the following warning:

Incompatible pointer types sending 'NSDate *' to parameter of type 'NSCalendar *'

This is the delegate definition:

@protocol CalendarViewDelegate <NSObject>
@optional
- (void) selectDateChanged:(NSCalendar *) selectDate;
- (void) monthChanged:(NSCalendar *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height;
- (void) beforeMonthChange:(CalendarView *) calendarView willto:(NSCalendar *) currentMonth;
@end

Delegates are one of my weak points; for some reason I can't seem to understand them, although I understand what their purpose is. When I look in my code for monthChanged, I find nothing! So my question is: why is the author using this code if it doesn't do anything? And how do I change the code to remove the warning, which is affecting the correct operation of the affected methods?

SpokaneDude
  • 4,856
  • 13
  • 64
  • 120

1 Answers1

1

- (void) monthChanged:(NSCalendar *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height; expects an NSCalendar as the first arg and you are passing a NSDate. Either you are passing a date when you should be passing a calendar or the delegate method should be updated to take a date arg and not an NSCalendar argument. Did you write those delegate methods?

If Self is a NSCalendar then probably should be called

[calendarViewDelegate monthChanged:self viewLeftTop:self.frame.origin height:height];

Else if you meant to pass an NSDate

@protocol CalendarViewDelegate <NSObject>
@optional
- (void) selectDateChanged:(NSDate *) selectDate;
- (void) monthChanged:(NSDate *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height;
- (void) beforeMonthChange:(CalendarView *) calendarView willto:(NSDate *) currentMonth;
@end

UPDATE

Example implementation of a delegate method in the delegate if say MYView needs to show an alert every time date changes

@interface MYView ()<CalendarViewDelegate>
@end
@implementation MYView
- (void)viewDidLoad{
    self.calendarView = [[CalendarView alloc] init];
    self.calendarView.delegate = self;
}
- (void) monthChanged:(NSDate *) currentMonth viewLeftTop:(CGPoint)viewLeftTop height:(float)height{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"date changed" andBlahBlah];
    [alert show];
}
@end
Alex Reynolds
  • 6,264
  • 4
  • 26
  • 42
  • I didn't write the delegate methods; as a matter of fact, I can't find them in the entire app! (I can't find *any* code for those methods! That's what's got me confused... – SpokaneDude Feb 13 '15 at 17:54
  • 2
    Delegate methods don't have to be implemented. These are listed as optional so there may be no code that uses them. The warning is just a warning that you are passing different types. Just either rewrite the delegate method signatures to take an NSDate or when you call the method pass in an NSCalendar – Alex Reynolds Feb 13 '15 at 17:58
  • `CalendarView ` notifies it delegate that stuff was changed. If no other object cares then there is no need to implement the delegate methods. If say `MyView` wants to show the user an alert when date changes then `MyView` would do `calendarView.delegate = self` and then you would write code to show an alert when `MyView` implements those delegate methods – Alex Reynolds Feb 13 '15 at 18:00