objective-cmacosnsdatepicker

How to show year only in NSDatePicker


I want to show year component only in a NSDatePicker.

I tried set/add NSDateFormatter to NSDatePicker, but with no luck. (By dragging a NSDateFormatter to NSDatePicker in nib)

Is there any way to achieve this without subclassing NSDatePicker or NSTextField? Thank you!


Solution

  • -[NSDatePicker setDatePickerElements:] can no longer be tricked into showing year only.

    A year-only NSDatePicker can be obtained by configuring a year-month picker and subclassing NSDatePickerCell to prevent it from creating the month and separator fields:

    - (void)_addSubfieldForElement:(int)arg1 withDateFormat:(id)arg2 referenceStrings:(id)arg3
    {
        if ((arg1 == 6) || (arg1 == 100)) {
            return;
        }
    
        [super _addSubfieldForElement:arg1 withDateFormat:arg2 referenceStrings:arg3]; // Private API
    }
    

    Please file bug reports with Apple to request a year-only variant of NSDatePicker. I also requested a week-of-year picker.