Pregunta

I've searched high and low for the answer to this, but alas, I'm here. As I know that NSDateFormatter objects are costly to create, I don't want to instantiate one for every NSTextField inside of my table. The problem I'm facing is that my table is view-based (as opposed to cell-based), and when attempting to connect a shared instance of a date formatter, I get the yellow warning symbol and this message:

Unsupported Configuration: Outlet 'formatter' of 'Text Field Cell - Table View Cell' is connected to 'Date Formatter,' an invalid destination (Objects inside view based table views may only be connected to the table view's delegate.)

The problem with that warning is this: the above referenced "Date Formatter" is a property of "File's Owner," which also happens to be the NSTableView's delegate. Am I missing something here, or am I going to have to create formatters for every date-related text field in my table?

¿Fue útil?

Solución

You can attach a shared formatter programmatically instead of in the xib file. In your delegate method for returning the view for the cell, supposing that your cell class is MyCellView and MyCellView has a property theTextField, and the view is loaded from a xib file named MyCellView.xib:

- (NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row {
    MyCellView *result = [tableView makeViewWithIdentifier:@"MyCellView" owner:self];
    [result.theTextField setFormatter:[[self class] sharedFormatter]];
    return result;
}

Then you need this sharedFormatter method. The sharedFormatter method will set up a lazily initialized singleton. This is a pretty common pattern in iOS and Mac OS X development, and it's a good one to learn if you haven't seen it before:

+ (NSFormatter *)sharedFormatter {
    static NSFormatter *formatter;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        formatter = [[NSFormatter alloc] init];
        /* Set up the formatter's attributes here */
    });
    return formatter;
}

So sharedFormatter initializes its static formatter variable at most once (the first time the sharedFormatter method is called). The dispatch_once causes all the code in its input block to be executed only one time, and even takes care of synchronizing calls occurring on multiple threads (though I doubt you'd be calling sharedFormatter from multiple threads).

Otros consejos

you should not creat formatters for every date-related text field .use Singleton can work it out.

add this code to your viewController

    + (NSDateFormatter *)sharedDateFormatter
    {

        NSLog(@"%s",__func__);
        static dispatch_once_t once;
        static NSDateFormatter * _sharedDateFormatter;

        dispatch_once(&once, ^{
            _sharedDateFormatter  = [[NSDateFormatter alloc] init];
        });
        return _sharedDateFormatter;


    }

,then you can access the NSDateFormatter Instance like this:

    [[self class] sharedDateFormatter],or ['Your ViewController class ' sharedDateFormatter]

you can add a property to your date-related textfield ,call this method to set the NSDateFormatter Instance into textfield:

    yourTextField.dateFormatter =  ['Your ViewController class ' sharedDateFormatter];

it will only creat one NSDateFormatter instance ,if you don`t understand ,please read this:

Create singleton using GCD's dispatch_once in Objective C

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top