【问题标题】:Change the null placeholder in a Cocoa binding?更改 Cocoa 绑定中的空占位符?
【发布时间】:2012-06-15 15:13:17
【问题描述】:

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

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

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

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

【问题讨论】:

    标签: cocoa interface-builder cocoa-bindings


    【解决方案1】:

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

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

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

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

    【讨论】:

    • 像魅力一样工作,谢谢!对于任何感兴趣的人,我一直在为所选值使用值转换器,它工作得很好,除了它被添加为菜单的最后一个选项,而 null 占位符设置在菜单的顶部。跨度>
    • 空值也会导致不同的可编辑/启用行为,因此使用值转换器您也需要维护可编辑和启用绑定。我想提供两种选择,但这种方式似乎更容易。
    • 这似乎对我不起作用。我将我的NSObjectController 子类化了,但没有调用该方法。
    • 视图创建与对象的绑定的唯一方法是调用该方法,因此出现了问题。你能发布一个新问题吗?
    【解决方案2】:

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

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

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

    【讨论】:

      【解决方案3】:

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

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

      NSPopUpButton 绑定:

      • 内容绑定到NSArrayController.arrangedObjects

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

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

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

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

      最后,在 Interface Builder 中选择 NSPopUpButton,然后在 Xcode Identity Inspector 中的 Custom Class 下选择您的 NSPopUpButton 子类。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-09-07
        • 2014-01-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-04
        • 2013-11-22
        • 1970-01-01
        相关资源
        最近更新 更多