【发布时间】:2013-10-29 13:04:27
【问题描述】:
我有一个自定义类“BackupIncrement”,它从 sqlite3 数据库中获取数据,并且工作正常。我还有一个名为“pubDefaultIncrement”的NSPopUpButton,它连接到IB。
最终结果是我希望popupbutton 填充有意义的标题,并且每个menuitem 或值都是数据库中的增量ID(主键)。
这里是有问题的代码:
- (void)doAddItemsTopubDefaultBackupIncrement {
marBackupIncrement = [[NSMutableArray alloc] init];
pubDefaultIncrement = [[NSPopUpButton alloc] init];
[pubDefaultIncrement removeAllItems];
mnuBackupIncrement = [[NSMenu alloc] init];
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)]];
NSMenuItem* mi = [[NSMenuItem alloc]
initWithTitle:[bi strIncrementValueDescription]
action:Nil
keyEquivalent:[bi strIncrementValue]];
[mi setTarget:self];
[mi setRepresentedObject:bi];
[mi setTag:[[bi intIncrementId] integerValue]];
[mnuBackupIncrement addItem:mi];
NSLog(@"Menu Item: %@", mi);
NSLog(@"Menu Item Title: %@", [mi title]);
NSLog(@"Menu Item Tag: %ld", (long)[mi tag]);
//[[pubDefaultIncrement menu] addItem:mi];
//[marBackupIncrement addObject:bi];
}
}
//NSLog(@"%@", mnuBackupIncrement);
//[pubDefaultIncrement setTarget:self];
[[pubDefaultIncrement menu] setSupermenu:mnuBackupIncrement];
[pubDefaultIncrement setMenu:mnuBackupIncrement];
}
sql 工作,它循环 5 次从 sqlite3 表中获取每一行。
这个:
NSLog(@"%@", mnuBackupIncrement);
打印出包含 5 个菜单项对象的菜单对象。
但是,当我运行我的项目代码时,弹出按钮有 3 个东西。
1st: a blank item
2nd: Item 2
3rd: Item 3
我最初尝试使用 ArrayController 和 Bindings 进行此操作,但我似乎无法让每个菜单项的标签正常工作。如果这是这样做的方法,我很乐意听取绑定的正确方法。
仅供参考。 BackupIncrement 对象公开了 4 个属性:
intIncrementId: the primary key from the table (1-5 currently for the 5 rows)
strIncrementValue: the single letter value from the table (h, d, w, m, y)
strIncrementDescription: the desc for the value (hour, day, week, month, year)
strIncrementValueDescription: a combination of the previous two - e.g. "d (day)", "m (month)"
So again - ideally id have the NSPopUpButton populate with menuitems which have strIncrementValueDescription as the visible "content" and the corresponding intIncrementId as the "value" such that when an item is selected I can get the tag or whichever an then act accordingly .
谁能指出我正确的方向?
【问题讨论】:
-
我认为你的方法有点不对劲。首先创建一个模型来包含来自 sqlite3 数据库的数据。此获取应该只进行一次。然后创建一个实现
NSMenuDelegate协议的对象,并将弹出按钮菜单委托设置为使用该对象。然后它可以将您的模型数据转换为菜单。
标签: objective-c cocoa sqlite nspopupbutton