有没有办法更改(出于本地化目的)Cocoa绑定中的空占位符?
绑定是在界面生成器中为弹出按钮设置的。在 IB 中设置的绑定的双向性质是必需的,因此以编程方式执行此操作并不真正吸引人。
我知道处理 nib 文件本地化的标准方法是为每种语言创建一个,但由于这是语言版本之间整个 nib 文件中的唯一区别,因此对于单个字符串来说似乎有点过分。
如果有一种方法可以修改在 IB 中创建的绑定,我正在考虑在文件所有者的 awakeFromNib
方法中执行此操作。
在绑定到的控制器对象(如 NSDocument 类)中,重写 -bind:toObject:withKeyPath:options:
。这需要成为该方法调用的目标 - 您在 nib 中的"绑定到:"下选择的对象。
如果绑定到 NSObjectController 或 NSArrayController,则需要一个子类。
该方法应重写options
字典并调用 super,将 NSNullPlaceholderBindingOption
的值替换为本地化字符串。
我会省略 nib 中的 null 占位符和代码中的键值,尽管您当然可以获取该键的传入值并对其进行翻译。
另一个答案似乎不再有效,因此我想出了一个略有不同的解决方案,该解决方案修改现有绑定以使用给定的空占位符字符串:
我的视图控制器中有此方法:
- (void)rebind:(NSString *)binding of:(id)object withNullPlaceholder:(NSString *)nullPlaceholder {
// Possibly a bad idea, but Xcode doesn't localize the null placeholder so we have do it manually.
NSDictionary *bindingInfo = [object infoForBinding:binding];
id bindObject = bindingInfo[NSObservedObjectKey];
NSString *keyPath = bindingInfo[NSObservedKeyPathKey];
NSMutableDictionary *options = [bindingInfo[NSOptionsKey] mutableCopy];
options[NSNullPlaceholderBindingOption] = nullPlaceholder;
[object unbind:binding];
[object bind:binding toObject:bindObject withKeyPath:keyPath options:options];
}
对于所有需要它的绑定,我awakeFromNib
调用它并传入本地化字符串:
- (void)awakeFromNib {
// Hacky hack hack: Xcode is stupid and doesn't localize the null placeholders so we have to do it.
[self rebind:@"contentValues" of:self.fooPopup withNullPlaceholder:NSLocalizedString(@"No foos available", @"foo popup null placeholder")];
[self rebind:@"contentValues" of:self.barPopup withNullPlaceholder:NSLocalizedString(@"No bars available", @"bar popup null placeholder")];
}
然后,本地化的字符串将作为Localizable.strings
文件的一部分进行正常本地化。
我能够在使用绑定的NSPopUpButton
中更改空占位符字符串(即"无值")。
具体来说,我想有一个弹出按钮菜单项,其标题不是"无值",表示对象为 nil
。选择空占位符菜单项时,应在用户默认值中保存空NSString
或nil
。
NSPopUpButton Bindings:
-
内容绑定到
NSArrayController.arrangedObjects
-
内容对象绑定
NSArrayController.arrangedObjects.exampleContentObject
(NSString
),这是菜单项所代表的对象,是Selected Object
, -
内容值绑定到
NSArrayController.arrangedObjects.exampleContentValue
(NSString
),这是弹出按钮菜单项中显示的标题。 -
弹出按钮的选定对象绑定到与
Content Objects
和Selected Object
相同的对象类型NSSharedUserDefaultsController.values.ExampleUserDefaultsKey
(NSString
)。此对象应与绑定中指定的 NSUserDefault 键的对象类型匹配。当从弹出按钮中选择项目时,它将所选对象保存为用户默认值。
要将空占位符字符串从"无值"更改为其他内容,子类NSPopUpButton
并覆盖-[NSPopUpButton bind:toObject:withKeyPath:options:]
。
@interface CustomPopUpButton : NSPopUpButton
@end
@implementation CustomPopUpButton
- (void)bind:(NSString *)binding toObject:(id)observable withKeyPath:(NSString *)keyPath options:(NSDictionary<NSString *,id> *)options {
NSMutableDictionary *mutableOptions = options ? [options mutableCopy] : [NSMutableDictionary dictionaryWithCapacity:1];
mutableOptions[NSInsertsNullPlaceholderBindingOption] = @YES;
mutableOptions[NSNullPlaceholderBindingOption] = @"Custom Null Placeholder Text";
[super bind:binding toObject:observable withKeyPath:keyPath options:[mutableOptions copy]];
}
@end
最后,在界面生成器中选择NSPopUpButton
,然后在 Xcode 身份检查器的自定义类下选择NSPopUpButton
子类的类。