3

有没有办法改变(为了本地化)Cocoa 绑定中的空占位符?

绑定是在 Interface Builder 中为弹出按钮设置的。需要在 IB 中设置的绑定的双向性质,因此以编程方式执行此操作并不是很吸引人。

我知道处理 nib 文件本地化的标准方法是为每种语言制作一个,但由于这是整个 nib 文件在语言版本之间的唯一区别,因此单个字符串似乎有点过分。

如果有办法修改在 IB 中创建的绑定,我正在考虑在文件所有者的awakeFromNib方法中进行。

4

3 回答 3

5

在您绑定的控制器对象中,例如您的 NSDocument 类,覆盖-bind:toObject:withKeyPath:options:. 这需要是该方法调用的目标 - 您在Bind to:下选择的对象:在 nib 中。

如果你绑定到一个 NSObjectController 或 NSArrayController,你需要一个子类。

该方法应该重写字典并调用 super,用您的本地化字符串options替换值。NSNullPlaceholderBindingOption

我会省略 nib 中的 null 占位符和代码中的键值,尽管您当然可以获取该键的传入值并进行转换。

于 2012-06-15T15:40:48.557 回答
0

另一个答案似乎不再起作用,所以我想出了一个稍微不同的解决方案,它修改现有绑定以使用给定的空占位符字符串:

我的视图控制器中有这个方法:

- (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文件的一部分进行本地化。

于 2016-04-18T22:19:59.027 回答
0

我能够更改NSPopUpButton使用绑定的空占位符字符串(即“无值”)。

具体来说,我想要一个弹出按钮菜单项,其标题不是“无值”,表示对象为nil. 选择空占位符菜单项时,应将空的NSString或保存在用户默认值中。nil

NSPopUpButton 绑定:

  • 内容绑定到NSArrayController.arrangedObjects

  • 内容对象被绑定 NSArrayController.arrangedObjects.exampleContentObjectNSString),这是菜单项所代表的对象,Selected Object

  • 内容值绑定到NSArrayController.arrangedObjects.exampleContentValue( NSString),这是弹出按钮的菜单项中显示的标题。

  • 弹出按钮的选定对象与和( )绑定到NSSharedUserDefaultsController.values.ExampleUserDefaultsKey的对象类型相同。该对象应与绑定中指定的 NSUserDefault 键的对象类型匹配。当从弹出按钮中选择一个项目时,它会将选定对象保存为用户默认值。Content ObjectsSelected ObjectNSString

要将空占位符字符串从“无值”更改为其他内容,请 subclassNSPopUpButton和 override -[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

最后,在 Xcode Identity Inspector 中的NSPopUpButtonInterface Builder 和Custom Class下选择你的类到 NSPopUpButton子类。

于 2018-09-13T21:33:58.953 回答