【问题标题】:Programmatically load an NSComboBox with keys/values (or bindings?)以编程方式加载带有键/值(或绑定?)的 NSComboBox
【发布时间】:2013-10-22 02:44:12
【问题描述】:

我有一个加载了访问的 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 个公开的实例变量作为 incrementid、value 和 description 的属性。并且打算尝试加载一个 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 中的绑定的帮助将不胜感激!谢谢!

【问题讨论】:

    标签: objective-c xcode cocoa-bindings nscombobox


    【解决方案1】:

    我发现我的问题归根结底是时机问题。我最终切换到了 NSPopupButton,但这是我最终的结果: programatically adding menu items to nspopupbutton

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-09-03
      • 2012-12-03
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 2012-03-14
      • 1970-01-01
      相关资源
      最近更新 更多