Configure NSDatePicker Delegate

I have NSDatePicker (NSDatePicker* datePicker)and set its delegate as the main application ( self). I programmed the following.

  • Make selfdelegate datePicker

    [[datePicker cell] setDelegate:self];

  • Set datePickerAction:to call when the control is pressed.

    [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.

enter image description here

  • Q: What is wrong with this delegation? The target / action method works fine.
  • Q: What document can I use for the delegation method for NSDatePicker?
+3
source share
4 answers

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.

+3

NSDatePicker. , NSDatePicker , NSControl, NSDatePicker.

. NSDatePicker, ( NSDatePickerCellDelegate), . , .

NSDatePicker , , . , target/action - .

+2

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
0
source

Set the delegate itself NSDatePickerinstead of your cell:

[datePicker setDelegate:self]

-4
source

All Articles