在基于视图的NSTableView中的NSTextField之间共享NSDateFormatter? [英] Sharing an NSDateFormatter between NSTextFields in view-based NSTableView?

查看:109
本文介绍了在基于视图的NSTableView中的NSTextField之间共享NSDateFormatter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找答案,但可惜,我在这里.我知道NSDateFormatter对象的创建成本很高,所以我不想为表中的每个NSTextField实例化一个对象.我面临的问题是我的表是基于视图的(而不是基于单元格的),并且当尝试连接日期格式化程序的共享实例时,出现黄色警告符号和以下消息:

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

该警告的问题是这样的:上面引用的日期格式化程序"是文件所有者"的属性,该属性也恰好是NSTableView的委托.我在这里丢失了什么吗?还是我必须为表中每个与日期相关的文本字段创建格式器?

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?

推荐答案

您可以以编程方式而不是在xib文件中附加共享格式化程序.在用于返回单元格视图的委托方法中,假设您的单元格类为MyCellView并且MyCellView具有属性theTextField,并且视图是从名为MyCellView.xib的xib文件中加载的:

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

然后您需要此sharedFormatter方法. sharedFormatter方法将设置延迟初始化的单例.在iOS和Mac OS X开发中,这是一种非常常见的模式,如果您以前没有看过它,这是一个很好的学习方法:

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

因此,sharedFormatter最多一次初始化其静态formatter变量(第一次调用sharedFormatter方法). dispatch_once导致其输入块中的所有代码仅执行一次,甚至负责同步发生在多个线程上的调用(尽管我怀疑您会从多个线程中调用sharedFormatter).

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

这篇关于在基于视图的NSTableView中的NSTextField之间共享NSDateFormatter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆