gpt4 book ai didi

macos - NSPrintInfo printSettings 不符合 KVO 标准,尽管标题中有评论这么说

转载 作者:行者123 更新时间:2023-12-03 16:29:11 26 4
gpt4 key购买 nike

背景:我正在向打印对话框添加一个打印面板附件 View (使用 addAccessoryController:),其中控件绑定(bind)到 NSPrintInfo printSettings 值,以便这些值保存在打印预设中。我在观察 printSettings 更改时遇到困难。我正在使用 SDK 10.6 进行构建,并在 OS X 10.7 上进行测试。

这是一个按照我的理解应该可以工作的代码示例,但是 observeValueForKeyPath: 从未被调用:

- (void)testKVO
{
NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
[printInfo addObserver:self forKeyPath:@"printSettings.foo" options:0 context:NULL];
[printInfo setValue:@"bar" forKeyPath:@"printSettings.foo"]; // observeValueForKeyPath:ofObject:change:context: not called
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%s %@ :: %@", _cmd, keyPath, object);
}

我也尝试直接观察 printSettings,但没有再成功,观察者方法也没有被调用(NSPrintInfo 返回的 printSettings 实际上是类 NSPrintInfoDictionaryProxy):

- (void)testKVO
{
NSMutableDictionary *printSettings = [[NSPrintInfo sharedPrintInfo] printSettings];
[printSettings addObserver:self forKeyPath:@"foo" options:0 context:NULL];
[printSettings setValue:@"bar" forKey:@"foo"]; // observeValueForKeyPath:ofObject:change:context: not called
}

我仔细检查了我的 KVO 系统是否在正常条件下工作并调用了观察者方法:

- (void)testKVO
{
NSMutableDictionary *printSettings = [NSMutableDictionary dictionary];
[printSettings addObserver:self forKeyPath:@"foo" options:0 context:NULL];
[printSettings setValue:@"bar" forKey:@"foo"]; // observeValueForKeyPath:ofObject:change:context: called at last!
}

所以问题是:如何观察 printSettings 的修改,尤其是了解用户何时选择了打印预设?

我还希望预览能够自动更新

- (NSSet *)keyPathsForValuesAffectingPreview
{
return [NSSet setWithObjects:
@"representedObject.printSettings.foo",
nil];
}

预览更新有一个简单的解决方法:通过直接在 NSViewController 本身上重新声明属性来添加间接级别。但对于打印预设的更改我不知道。

最后,这是 NSPrintInfo.h 中的注释:

- (NSMutableDictionary *)printSettings;

The print info's print settings. You can put values in this dictionary to store them in any preset that the user creates while editing this print info with a print panel. Such values must be property list objects. This class is key-value coding (KVC) and key-value observing (KVO) compliant for "printSettings" so you can often bind controls in print panel accessory views directly to entries in this dictionary. You can also use this dictionary to get values that have been set by other parts of the printing system, like a printer driver's print dialog extension (the same sort of values that are returned by the Carbon Printing Manager's PMPrintSettingsGetValue() function). Other parts of the printing system often use key strings like "com.apple.print.PrintSettings.PMColorSyncProfileID" but dots like those in key strings wouldn't work well with KVC, so those dots are replaced with underscores in keys that appear in this dictionary, as in "com_apple_print_PrintSettings_PMColorSyncProfileID". You should use the same convention when adding entries to this dictionary.

感谢任何帮助

谢谢

最佳答案

好吧,我找到了办法。当选择打印预设或更改纸张格式时,会发送一条未记录的通知,您所要做的就是添加一个观察者:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(printInfoDidChange:) name:@"NSPrintInfoDidChange" object:nil];

这并不像绑定(bind)到 printSettings 键路径那么简单,而且我真的不喜欢使用未记录的通知(在可维护性方面几乎与使用私有(private) API 一样糟糕),但这是我能找到的唯一方法工作。

关于macos - NSPrintInfo printSettings 不符合 KVO 标准,尽管标题中有评论这么说,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11047459/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com