以编程方式加载带有键/值(或绑定?)的NSComboBox

时间:2013-10-15 00:50:13

标签: objective-c xcode cocoa-bindings nscombobox

我有一个sqlite3数据库加载了一个被访问的,我试图将下表的内容放入NSComboBox(然后自动选择其他操作参数的默认键)。这是表格内容:

(table) _backup_increment:
('incrementid', 'value', 'description')
(1, 'h', 'hours')
(2, 'd', 'days')
(3, 'w', 'weeks')
(4, 'm', 'months')
(5, 'y', 'years')

目标是以某种方式(理想情况下在代码中,但如果必须使用绑定,或者如果它被认为是更好的做法)将这些项加载到NSComboBox中,如下所示:

      _______________________
key:  [ (Select Value)   |\/]
      -----------------------
key:1 | h (hours)           |
key:2 | d (days)            |
key:3 | w (weeks)           | (* auto select this one as the default for now)
key:4 | m (months)          |
key:5 | y (years)           |
      -----------------------

我在某个时候创建​​了一个BackupIncrement类,其中包含3个公开的实例变量作为增量,值和描述的属性。并且打算尝试加载NSMutableArray并尝试以编程方式将其作为数据源链接,但我似乎无法使其正常工作。到目前为止,这是我的代码,你可以看到我在玩对象和可变数组的位置:

-(void)doAddItemsTocmbDefaultBackupIncrement {
    const char* chrSQLSelect = "SELECT * FROM _backup_increments";
    sqlite3_stmt* compiledSQL;
    if (sqlite3_prepare_v2(claAppDelegate.sl3Database, chrSQLSelect, -1, &compiledSQL, NULL) == SQLITE_OK) {
        while (sqlite3_step(compiledSQL) == SQLITE_ROW) {
            BackupIncrement* bi = [[BackupIncrement alloc]
                                initWithintIncrementId:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledSQL, 0)]
                                AndstrIncrementValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledSQL, 1)]
                                AndstrIncrementDescription:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledSQL, 2)]];
            //[cmbDefaultBackupIncrement addItemWithObjectValue:[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledSQL, 1)]];
            [cmbDefaultBackupIncrement addItemWithObjectValue:bi];
            [marBackupIncrement addObject:bi];
//          NSLog(@"%ld: %@(%@)",
//                  (long)[[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledSQL, 0)] integerValue],
//                  [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledSQL, 1)],
//                  [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledSQL, 2)]);
//          NSLog(@"===========");
        }
        for (id obj in marBackupIncrement)
            NSLog(@"obj: %@", [obj valueForKey:@"intIncrementId"]);
        [cmbDefaultBackupIncrement reloadData];
        //NSLog(@"What? %@", marBackupIncrement);
    }
}

所以是的 - 任何以编程方式或在XCode5中使用Bindings的帮助都将非常感谢!谢谢!

1 个答案:

答案 0 :(得分:0)

我发现我的问题最终是时机问题。我最终切换到NSPopupButton,但这里是我最终的地方: programatically adding menu items to nspopupbutton