gpt4 book ai didi

macos - 是否可以在带有 ScreenSaverDefaults 的 Nib 中使用绑定(bind)?

转载 作者:行者123 更新时间:2023-12-01 23:43:11 24 4
gpt4 key购买 nike

我正在开发屏幕保护程序,所以我应该使用 ScreenSaverDefaults 而不是 NSUserDefaults。我想让我的配置面板为其 UI 使用绑定(bind),但它们需要连接到 ScreenSaverDefaults,我看不到这样做的方法; IB (xCode) 中唯一可用的默认 Controller 是标准用户默认 Controller 。是否有解决方法,或者只是不能在屏幕保护程序的上下文中使用 Nib 中的绑定(bind)?

最佳答案

我已经成功地在屏幕保护程序中使用了绑定(bind)。

(可能重要的说明:我仍在使用 XC 3.2.6 在 10.6 上进行开发;到目前为止,此代码仅在 10.10 上进行了轻微测试(但似乎确实有效)。另外,我混合了 C++ 和 Objective-C,所以你可能需要进行少量代码清理,如下所示。)

我的代码创建并且 UI 绑定(bind)到自定义用户默认 Controller (控件绑定(bind)到 File's Owner,它设置为我的屏幕保护程序 View , Controller 键为空,模型键路径为 self.defaultsController.values.key_val ,其中 key_val 是默认 plist 中用于访问绑定(bind)到控件的值的任何键)。

您还必须创建自己的屏幕保护程序默认值,并在屏幕保护程序初始化的早期(例如在屏幕保护程序 View 的 initWithFrame: 方法中)指导您的自定义用户默认值 Controller 使用它们,如下所示:

// Find our bundle:
NSBundle * screenSaverBundle = [NSBundle bundleForClass: [self class]];

// Load per-user screen saver defaults:
ScreenSaverDefaults * screenSaverDefaults = [ScreenSaverDefaults defaultsForModuleWithName: [screenSaverBundle bundleIdentifier]];

// Create default defaults (values to use when no other values have been established):
NSDictionary * defaultDefaultValues = [self defaultDefaults];

// Register default defaults as fallback values (in registration domain):
[screenSaverDefaults registerDefaults: defaultDefaultValues];

// Configure custom user defaults controller to use the correct defaults:
// NOTE: defaultsController of NSUserDefaultsController * type *MUST* be declared using @property in your header, and accessors must be provided (typically using @synthesize in the implementation section).
defaultsController = [[NSUserDefaultsController alloc] initWithDefaults: screenSaverDefaults initialValues: nil]; // <- change nil to factory defaults, if desired

// Make sure changes are only saved when committed by user:
[defaultsController setAppliesImmediately: false];

(上面的代码是根据我自己代码中的各种方法稍微重新排列的;可能有一两个错字,但要点是正确的。)

defaultDefaults 的实现看起来像这样(请原谅我的非传统风格):
- (NSDictionary *) defaultDefaults
{
NSString * ResourcesPath = [[self screenSaverBundle] resourcePath];
NSString * DefaultDefaultsPath = [ResourcesPath stringByAppendingPathComponent: @"DefaultDefaults.plist"];

NSDictionary * DefaultDefaults = [NSDictionary dictionaryWithContentsOfFile: DefaultDefaultsPath];

return DefaultDefaults;
}

如果您想在屏幕保护程序中提供“恢复出厂设置”功能,则需要在创建自定义用户默认 Controller 时将 initialValues: 参数设置为“原始出厂值”字典。为了响应“重置为出厂默认设置”按钮,只需将 revertToInitialValues: 发送到 Controller 。

最后,请注意,根据上面代码中创建的某些对象的预期生命周期,您可能需要保留其中的一些(并在以后适本地释放它们)。我假设你理解这个 bhaller;我只是为广大观众指出这一点。

关于macos - 是否可以在带有 ScreenSaverDefaults 的 Nib 中使用绑定(bind)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30381923/

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