I have a custom class "BackupIncrement" which gets its data from an sqlite3 database, and works just fine. I also have an NSPopUpButton
called "pubDefaultIncrement" which is hooked up in IB
.
The end result is that I would like to have the popupbutton
populate with meaningful titles and also with each menuitem
or value being the increment id (primary key) from the database.
Here is the code in question:
- (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];
}
The sql works, it loops 5 times fetching each of the rows from the sqlite3 table.
This:
NSLog(@"%@", mnuBackupIncrement);
prints out the menu object with the 5 menu item objects inside it.
However when I run my project code the popupbutton has 3 things.
1st: a blank item
2nd: Item 2
3rd: Item 3
I was originally attempting this with an ArrayController and Bindings but I couldn't seem to get the tags for each menu item to work properly. If that is the way to do this, I would gladly listen to the proper way to the bindings.
Just f.y.i. the BackupIncrement object exposes 4 properties:
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.
Can anyone point me in the right direction?
As it turns out (once again) my issue was a timing one. I was calling the function in question from within the ViewController's 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;
}
When instead I should have been calling it after the ViewController has been loaded:
- (void)loadView {
[self viewWillLoad];
[super loadView];
[self viewDidLoad]; // <-- call in here instead
}
- (void)viewDidLoad {
[self doAddItemsTopubDefaultBackupIncrement]; //<-- HERE: works just fine...
[pubDefaultIncrement selectItemWithTag:claAppDelegate.intDefaultBackupIncrmentId];
}