1

我有一个自定义类“BackupIncrement”,它从 sqlite3 数据库中获取数据,并且工作得很好。我还有一个NSPopUpButton名为“pubDefaultIncrement”的连接在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.

谁能指出我正确的方向?

4

1 回答 1

2

事实证明(再次)我的问题是时间问题。我从 ViewController 的 initWithNibName 中调用了有问题的函数...

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
        if (!claAppDelegate) {
            claAppDelegate = [[AppDelegate alloc] init];
        }
        //[self doAddItemsTopubDefaultBackupIncrement]; <--- HERE doesn't work....
    }
    return self;
}

相反,我应该在加载 ViewController后调用它:

- (void)loadView {
    [self viewWillLoad];
    [super loadView];
    [self viewDidLoad]; // <-- call in here instead
}

- (void)viewDidLoad {
    [self doAddItemsTopubDefaultBackupIncrement]; //<-- HERE: works just fine...
    [pubDefaultIncrement selectItemWithTag:claAppDelegate.intDefaultBackupIncrmentId];
}
于 2013-10-21T18:38:27.710 回答