【问题标题】:Programmatically adding menu + items to NSPopupButton以编程方式将菜单+项目添加到 NSPopupButton
【发布时间】: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


【解决方案1】:

事实证明(再次)我的问题是时间问题。我是从 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];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    • 2015-01-04
    相关资源
    最近更新 更多