以编程方式将菜单+项添加到NSPopupButton

时间:2013-10-21 06:39:00

标签: objective-c cocoa sqlite nspopupbutton

所以,我有一个自定义类“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,但我似乎无法让每个菜单项的标签正常工作。如果这是这样做的方式,我很乐意听取绑定的正确方法。

就是f.y.i. 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)"

所以再次 - 理想情况下,id使NSPopUpButton填充了menuitems,其中strIncrementValueDescription作为可见的“content”,相应的intIncrementId作为“value”,这样当选择一个项目时我可以得到标签或者相应的行为

有人能指出我正确的方向吗?

谢谢!

1 个答案:

答案 0 :(得分: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];
}