請幫幫忙,這是推動我瘋了:(恢復挑選NSPopUpButton上加載表單
我有一個表格作爲OSX Cocoa程序我正在寫它使用一個NSPopUpButton選擇記錄類型的一部分要創建/編輯。
我使用綁定彈出按鈕綁定到陣列控制器和我的選擇值綁定到一個NSString。
我可以彈出填充,可以選擇一個項目,保存記錄沒問題,但有兩件事情我不明白:
1)選定的值對象包含項目的文本名稱而不是它的鍵 - 我已經設置了內容對象綁定
2)我無法在重新加載窗體時恢復彈出窗口的選定項目。
下面是一些代碼樣本,以協助:
@implementation片段:
IBOutlet NSPopUpButton *popMediaType;
IBOutlet NSArrayController *acMediaTypes;
@property (nonatomic, copy) NSString *selMediaType;
@interface片段:
- (void)setupForm {
NSArray *types = [[NSArray alloc] initWithObjects:[NSDictionary dictionaryWithObjectsAndKeys:@"Video", @"name", @"video", @"value", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Advert", @"name", @"advert", @"value", nil],
[NSDictionary dictionaryWithObjectsAndKeys:@"Graphic", @"name", @"graphic", @"value", nil],
nil];
[acMediaTypes setContent:types];
}
在Interface Builder:
acMediaTypes是參照出口用於稱爲mediaTypes的數組控制器對象popMediaType是用於NSPopUpButton窗體上的引用出口
我NSPopUpButton的綁定是這樣構成的:
Content (mediaTypes.arrangedObjects)
Bind to: mediaTypes
Controller Key: arrangedObjects
Model Key Path: <null>
Content Objects (mediaTypes.arrangedObjects)
Bind to: mediaTypes
Controller Key: arrangedObjects
Model Key Path: value
Content Values (mediaTypes.arrangedObjects)
Bind to: mediaTypes
Controller Key: arrangedObjects
Model Key Path: name
Selected Value (File's Owner.selMediaType)
Bind to: Files Owner
Controller key: <null>
Model Key Path: selMediaType
彈出顯示正確,示出了從陣列的適當的「名稱」部分的文本。
如果我在我的代碼中使用類似NSLog()的方式讀取selMediaType
,那麼我會看到「視頻」,例如,如果選擇了該項目 - 它應該是「視頻」(小寫字典中的「值」部分)
我可以在我的sqlite數據庫中存儲沒有問題的值,並從終端使用sqlite3檢查它們,並保存所有內容。
如果我從表中加載數值,我會認爲只需設置selMediatype
就可以重置NSPopUpButton中的項目位置,但它不會。