user2417624
user2417624

Reputation: 693

How to replace programmatically "No Selection" placeholder in NSTextField that uses binding?

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.

Upvotes: 0

Views: 366

Answers (1)

Willeke
Willeke

Reputation: 15589

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}];

Upvotes: 1

Related Questions