- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
背景:我正在向打印对话框添加一个打印面板附件 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/
当我的应用程序关闭时,主 Controller 类将其自身作为观察者从模型中删除,然后释放模型。像这样: - (void)dealloc { [theModel removeObserver:
我正在努力加深对这些机制的理解。我有一个启用触摸的 UIView,可以更新它自己的 center 属性。 我的理解是frame属性是center和bounds的合成函数。假设这是真的,我将一个观察者放
我正在尝试使用 KVO 来观察在我的页面 View Controller 的子内容 View Controller 的 ScrollView 中使用拖动时的更新变化,但是当应用程序启动时,它崩溃了说:
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this qu
我尝试使用 KVO 观察 UIView 子类上的属性,以便通过调用 drawRect: 触发绘制。在我的 initWithFrame: 中,我有这个: ... self.observedKeysTha
我以前使用过 NSNotifications,但这是我第一次尝试在 Cocoa Touch 中使用 KVO。 我的 UITableView Controller 在各种数据源之间切换,因此我将它们封装
我遇到了 KVO 问题,但我不知道它是什么。 我有一个可用于编辑或创建记录的屏幕。该记录由一个模型对象表示,该对象包含许多 NSString 类型的变量。 该字符串的 Controller 有一个实例
这个问题已经有答案了: In Cocoa KVO, why doesn't a change on a NSMutableArray proxy notify observers? (1 个回答) 已
当我想观察保留属性的变化时,我经常遇到这样的情况: @interface AnObserver {…} @property(retain) Foo *foo; 现在,当我想要设置观察时,我需要编写自己
我定义了两个具有相应 ivars 的属性并合成了它们: @property (nonatomic,copy) NSString* title; @property (nonatomic,copy) N
我试图掌握绑定(bind),并在 xcode 中设置了一个带有 CPTextView 的 CPWindow,并将窗口和 textview 绑定(bind)到它们各自的导出。 我有点卡住了,但是试图让
一定有一些我对 KVO 没有了解的地方,我试图通过滚动 UIScrollView 来滚动 UITableView,偏移量转换是正确的,但是 UITableView 滚动发生在 UIScrollView
我正在学习 MVVM,所以我在属性上添加了观察者 init(model: RegisterUserModel = RegisterUserModel()) { self.registerMo
我的 iPhone 应用程序核心数据属性的自定义访问器方法有问题。我想做的不仅仅是取出原始类型并返回它。我有一个可能的 activityTypes 的 NSSet,如果对象没有 primitiveTy
事情是这样的:我有一个单例监视设备的事件存储的变化。我有一个名为 events 的属性,我已将其包装在 eventsSignal RACSignal 中。 _eventsSignal = [RACAb
我得到的 iOS 应用 实体很少,它们由关系链接。举一个简单的例子,假设我们有这样的关系(一对多): company > person 我正在使用 xcode 4.6 和用于模型生成的核心数据建模工具
我在 iOS 应用程序中遇到 KVO 问题。我似乎无法正确处理。我正在尝试做什么: 我有一个 MenuViewController(可在左侧显示,如 facebook 应用程序)观察(并显示)全局值。
我试图通过以下代码从其他对象观察 UIViewController 的 KVO 的界面方向: [((UIViewController *)self.delegate) addObserver:self
我正在尝试使用 KVO 在 UILabel 和对象数据之间创建一个小型绑定(bind)系统。如果我的 UI 发生变化,我的数据也必须发生变化,如果我的数据发生变化,我的 UI 应该刷新以显示新值。 我
如果我在我的 iOS 应用程序上设置了本地通知,我将更新一个按钮。我想用 KVO 做到这一点,因为可以通过多种方式设置通知,所以我将无法轻松跟踪本地通知何时发生更改,否则。 我是这样设置的:
我是一名优秀的程序员,十分优秀!