更改Cocoa绑定中的空占位符?

时间:2012-06-15 15:13:18

标签: cocoa interface-builder cocoa-bindings

有没有办法在Cocoa的绑定中更改(出于本地化的目的)空占位符?

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

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

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

3 个答案:

答案 0 :(得分:5)

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

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

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

我会省略nib中的null占位符和代码中的键值,但是你当然可以获取该键的传入值并转换它。

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

答案 2 :(得分:0)

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

具体来说,我想有一个弹出按钮菜单项,该菜单项的标题不是“ No Value”,其对象为nil。选择空占位符菜单项时,应将空的NSStringnil保存在用户默认设置中。

NSPopUpButton绑定:

  • 内容已绑定到NSArrayController.arrangedObjects

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

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

  • 弹出按钮的
  • Selected Object (绑定对象)绑定到NSSharedUserDefaultsController.values.ExampleUserDefaultsKey,该对象类型与Content ObjectsSelected Object({{1 }})。该对象应与绑定中指定的NSUserDefault的键的对象类型匹配。从弹出按钮中选择一个项目后,会将 Selected Object (选择的对象)保存为用户默认设置。

要将空的占位符字符串从“无值”更改为其他内容,请子类NSString并覆盖NSPopUpButton


-[NSPopUpButton bind:toObject:withKeyPath:options:]

最后,在Interface Builder中,在Xcode Identity Inspector的 Custom Class 下,选择@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子类。

相关问题