NSPopUpButton
バインディングを使用する で、null プレースホルダー文字列 (つまり、「値なし」) を変更できました。
具体的には、オブジェクトがnil
. null プレースホルダー メニュー項目が選択された場合、空のNSString
orがユーザーの既定値に保存されます。nil
NSPopUpButton バインディング:
コンテンツはNSArrayController.arrangedObjects
コンテンツ オブジェクトはバインドされ ていますNSArrayController.arrangedObjects.exampleContentObject
( NSString
)。これは、メニュー項目が表すオブジェクトでありSelected Object
、
Content ValuesNSArrayController.arrangedObjects.exampleContentValue
は( ) にバインドされNSString
ます。これは、ポップアップ ボタンのメニュー項目に表示されるタイトルです。
ポップアップ ボタンの選択されたオブジェクトは、および( ) とNSSharedUserDefaultsController.values.ExampleUserDefaultsKey
同じオブジェクト タイプにバインドされます。このオブジェクトは、バインディングで指定された NSUserDefault のキーのオブジェクト タイプと一致する必要があります。ポップアップ ボタンから項目を選択すると、選択したオブジェクトがユーザーの既定値に保存されます。Content Objects
Selected Object
NSString
null プレースホルダー文字列を "No Value" から別のものに変更するには、サブクラス化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
Interface Builder で を選択し、Xcode Identity Inspector の Custom ClassNSPopUpButton
の下で、クラスをサブクラスにします。