2013-10-21 184 views
1

所以,我有一个自定义类“BackupIncrement”,它从sqlite3数据库获取其数据,并且工作得很好。我也有一个叫做“pubDefaultIncrement”的NSPopUpButton,它被连接到IB以编程方式将菜单+项目添加到NSPopupButton

最终结果是,我希望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菜单项的对象菜单对象。

但是,当我运行我的项目代码popupbutton有3件事情。

1st: a blank item 
2nd: Item 2 
3rd: Item 3 

我本来有ArrayController和绑定尝试这一点,但我不能似乎得到标记每个菜单项才能正常工作。如果这是做到这一点的方法,我会很乐意听到绑定的正确方法。

Just 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与具有strIncrementValueDescription作为可见的“内容”以及相应的intIncrementId为“值”的菜单项填充,使得当一个项目被选中我可以得到标签或其中的一个,然后采取相应的行动。

任何人都可以指向正确的方向吗?

谢谢!

+0

我觉得你的做法是稍微偏离。首先创建一个模型来包含来自sqlite3数据库的数据。这个提取只能完成一次。然后创建一个实现'NSMenuDelegate'协议的对象,并设置弹出式按钮菜单委托来使用这个对象。它可以将您的模型数据转换为菜单。 – trojanfoe

回答

2

事实证明(再次)我的问题是时间问题。我打电话从视图控制器的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; 
} 

当代替我应该已经后调用它的视图控制器已经被加载:

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

- (void)viewDidLoad { 
    [self doAddItemsTopubDefaultBackupIncrement]; //<-- HERE: works just fine... 
    [pubDefaultIncrement selectItemWithTag:claAppDelegate.intDefaultBackupIncrmentId]; 
} 
相关问题