I have NSDatePicker (NSDatePicker* datePicker)and set its delegate as the main application ( self). I programmed the following.
NSDatePicker (NSDatePicker* datePicker)
self
Make selfdelegate datePicker
[[datePicker cell] setDelegate:self];
Set datePickerAction:to call when the control is pressed.
datePickerAction:
[datePicker setAction:@selector(datePickerAction:)];
This is the method.
- (IBAction)datePickerAction:(id)sender { if( [[[NSApplication sharedApplication] currentEvent] modifierFlags] & NSShiftKeyMask ) NSLog(@"shift pressed %@", [datePicker dateValue]); else NSLog(@"hello %@", [datePicker dateValue]); }
The problem is that delegation does not work when I click the date in the NSDatePicker calendar.
You must add this method to your code:
- (void)datePickerCell:(NSDatePickerCell *)aDatePickerCell validateProposedDateValue:(NSDate **)proposedDateValue timeInterval:(NSTimeInterval *)proposedTimeInterval { NSString *aDate = [myDateFormat stringFromDate:*proposedDateValue]; }
IB DatePickerCell, , File Owner.
NSDatePicker. , NSDatePicker , NSControl, NSDatePicker.
. NSDatePicker, ( NSDatePickerCellDelegate), . , .
NSDatePicker , , . , target/action - .
You need to subclass the NSDatePicker and override the MouseDown event and associate it with the delegate. Here are the codes.
CustomDatePick.h
#import <Cocoa/Cocoa.h> @protocol CustomPickerDelegate - (void)didPickerClick; @end @interface CustomDatePicker : NSDatePicker @property (assign) id <CustomPickerDelegate> myDelegate; @end
CustomDatePick.m
#import "CustomDatePicker.h" @implementation CustomDatePicker - (void)mouseDown:(NSEvent *)event { [super mouseDown:event]; [self.myDelegate didPickerClick]; } @end
Set the delegate itself NSDatePickerinstead of your cell:
NSDatePicker
[datePicker setDelegate:self]