0

My app has one NSTableView on the left (master) and text fields on the right (details)

I use core data & binding to get the records displayed.

I have a text file that is bind with NSObjectController. It is a date field, and I have added NSDateFormatter in the text field.

My goal: to have a placeholder on that text field with the user system default date format. And I have achieved that with the following code

.h code: (only the relevant code is posted)

@property (weak) IBOutlet NSDateFormatter *offerExpiresOnDateFormatter;
@property (weak) IBOutlet NSTextField *offerExpiresOnDateTextField;

and the .m code:

-(void)viewWillAppear {

    [_offerExpiresOnDateFormatter setDateStyle:NSDateFormatterShortStyle];
    [_offerExpiresOnDateTextField setPlaceholderString:[_offerExpiresOnDateFormatter dateFormat]];

}

All this works.

My problem:

It works only when applications load (because I use viewWillAppear).

Placeholder is displayed in dd/mm/yy format, my system default format. However, if I click in empty rows in NSTableView, my placeholder disappears and "No Selection" placeholder is displayed.

How can I prevent this?

How can I display my dd/mm/yy placeholder when user click on empty row in NSTableView?

I can't just type dd/mm/yy in the NSTextField No Selection Placeholder under binding inspector, since i don't know what the user will have as system default.

Cœur
  • 37,241
  • 25
  • 195
  • 267
user2417624
  • 653
  • 10
  • 32
  • Try to drag and drop `NSDateFormatter` object from Object library to `NSTextField` in Storyboard and set date and time style in attribute Inspector. Comment out your code and try this way. – Khundragpan Feb 23 '16 at 03:50
  • Try binding No Selection Placeholder to key path `offerExpiresOnDateFormatter.dateFormat` of your view controller. – Willeke Feb 23 '16 at 10:13
  • @Willeke, can you please tell me how to do that? – user2417624 Feb 24 '16 at 02:53
  • @Khundragpan I already have NSDateformatter in my text field. However, when no record is selected in NSTableView, placeholder in all textfields is "No Selection" – user2417624 Feb 24 '16 at 02:55

1 Answers1

1

Don't bind the value in the XIB, bind it manually with options.

[self.offerExpiresOnDateTextField bind:NSValueBinding
    toObject:self.objectController
    withKeyPath:@"selection.date"
    options:@{NSNoSelectionPlaceholderBindingOption:
        self.offerExpiresOnDateFormatter.dateFormat}];
Willeke
  • 14,578
  • 4
  • 19
  • 47
  • I get an error [ valueForUndefinedKey:]: this class is not key value coding-compliant for the key date. I did unbind the textfield from XIB, and i place the above code in awakeFromNib method. I also tried the same code in the viewWillLoad and viewDidLoad, without any success. Any help? – user2417624 Feb 25 '16 at 01:09
  • Oops wrong again, I have to pay more attention. I edited my answer. – Willeke Feb 25 '16 at 01:37
  • Perfect. Works like a charm. Thanks. – user2417624 Feb 25 '16 at 01:46